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

I am trying to prepare a 301 redirect for a typo that I made "received"

I am struggling to find a way to get the url on behalf of the action and controller.

I know UrlHelper.Action, but it doesn't exist in Global.asax. How can I access this method ?:

// 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*/);
}

      

Alternatively I created a route, if that's how I should be getting the string, that's fine too, but I'm not sure how:

routes.MapRoute(
    name: "blah-list-received",
    url: "blah/list-received",
    defaults: new { controller = "Blah", action = "ListReceived" }
);

      

for example, it might look like this:

// 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" ) );
}

      

+3


source to share


1 answer


You need to build UrlHelper

yourself:

var url = new UrlHelper(HttpContext.Current.Request.RequestContext, RouteTable.Routes)
             .Action("YourAction",
                     "YourController",
                     new { paramName = paramValue });

      



See MSDN

+3


source







All Articles