Programming

Handle Legacy .aspx Page Requests In ASP.NET MVC

If you have a public web application that you are upgrading from Web Forms to ASP.NET MVC, you may find yourself wanting to capture requests to the now obsolete .aspx addresses.

By default, these requests won’t be passed to your application so you won’t be able to catch them using a route. So to get hold of them in your application you’ll need to make a change to your Web.config file to ensure .aspx requests are sent to your application:

<!-- ... -->
<system.webServer>
	<handlers>
	  <!-- ... -->
	  <add name="AspxHandler" path="*.aspx" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
	</handlers>
</system.webServer>

Once you have updated the Web.config, you can create a route to pass these requests to an action:

public static void RegisterRoutes(RouteCollection routes)
{
    // ...

    routes.MapRoute(
        name: "AspxRoute",
        url: "{permalink}.aspx",
        defaults: new { controller = "LegacyRedirection", action = "Aspx" }
    );
}

And within your controller you can do whatever you like with the request, for example redirect it to the new address, or display content, or whatever you like.

public class LegacyRedirectionController : Controller
{
    public ActionResult Aspx(string permalink)
    {
        ViewBag.PermaLink = permalink;
	//TODO: redirect, or fetch content - you choose! 
        return View();
    }
}