如何从 Global asax 中访问 UrlHelper.Action 或类似内容

How to access UrlHelper.Action or similar from within Global asax

我正在尝试为我输入的错误准备 301 重定向 'recieved'

我正在努力寻找一种从操作和控制器名称中获取 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