我正在尝试为我收到的“错字”准备301重定向
我正在努力寻找一种从动作和控制器名称获取url的方法.
我知道UrlHelper.Action,但它在Global.asax中不存在.如何获得此方法的访问权限:
// Add permanent redirection for retired pages (Application_BeginRequest())
if (HttpContext.Current.Request.Url.LocalPath.ToLower().StartsWith("/blah/listrecieved"))
{
HttpContext.Current.Response.RedirectPermanent(/*Need url generated from action and controller*/);
}
或者,我已经创建了一条路线,如果那是我应该如何获取字符串的方法,那也很好,但是我不确定如何:
routes.MapRoute(
name: "blah-list-received",
url: "blah/list-received",
defaults: new { controller = "Blah", action = "ListReceived" }
);
例如,它可能看起来像这样:
// Add permanent redirection for retired pages
if (HttpContext.Current.Request.Url.LocalPath.ToLower().StartsWith("/blah/listrecieved"))
{
HttpContext.Current.Response.RedirectPermanent(routes.GetUrl( "blah-list-received" ) );
}
解决方法:
您需要自己构造UrlHelper:
var url = new UrlHelper(HttpContext.Current.Request.RequestContext, RouteTable.Routes)
.Action("YourAction",
"YourController",
new { paramName = paramValue });
查看MSDN