ASP.Net MVC route to catch all *.aspx requests

Viewed 3384

This must have been asked before, but after reading here, here, here and here I can't extrapolate the relevant parts to make it work. I am revamping an old web forms site into MVC, and want to catch particular incoming HTTP requests so that I can issue a RedirectPermanent (to protect our Google rankings and avoid users leaving due to 404's).

Rather than intercept all incoming requests, or parse for some id value, I need to intercept all requests that end in (or contain) the .aspx file extension, e.g.

www.sample.com/default.aspx
www.sample.com/somedir/file.aspx
www.sample.com/somedir/file.aspx?foo=bar

Requests to the MVC routes should be ignored (just processed as normal).

Here's what I have so far, except the ASPXFiles route is never being hit.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // never generates a match
        routes.MapRoute(
            name: "ASPXFiles",
            url: "*.aspx",
            defaults: new { controller = "ASPXFiles", action = "Index" }
        );

        // Used to process all other requests (works fine)
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

}

Is this type of route possible to set up in MVC?

3 Answers

Since my situation I only had a few main pages, with cockamamy rules, I found this easier.. To create an "oldaspxcontroller". So I can be sure everything mapped correctly.

//https://www.oldsite.com/topics/travel/page9.aspx
[HttpGet]
[Route("/topics/{topic}/page{oldpagenum}.aspx")]
public LocalRedirectResult TopicWithPage(string topic, string oldpagenum)
{

    return LocalRedirectPermanent($"/topics/{topic}?p={oldpagenum}");
}

You may notice I still use pagenum in querystring.. I just think it looks better.. like mysite.com/topics/travel?p=9 I like better than mysite.com/topics/travel/page/9. I am in .Net core 3.1 and it works nice, even recognizes the pattern and the pagenumber..

Related