Search code examples
asp.netasp.net-mvcrouteswebformsignoreroute

Use System.Web.Mvc in non MVC ASP.NET Web Forms Application


We are developing an ASP.NET Web Forms application with REST modelled URL, for which we are using Route tables in global.asax. However, recently we came over a problem where the WebResource.axd and ScriptResource.axd files were getting routed as well. We wanted to use the RouteCollection.IgnoreRoute function. However, since it in System.Web.Mvc, it would mean that we would need to reference that dll in a non MVC Web Forms application.

Is it safe for us to continue with the approach. If anyone has a better way to ignore routes from within the Web Forms application please do share. Please also note that the application has been extensively developed in Web Forms and moving to MVC at the current stage is not feasible.


Solution

  • For web forms you need to use System.Web.Routing

    Put this is Global.asax

    void RegisterRoutes(System.Web.Routing.RouteCollection routes)
    {
        routes.Ignore("{resource}.axd/{*pathInfo}");
    
        routes.MapPageRoute(
           "Home",      // Route name
           "WWWWWW",      // Route URL
           "~/Default.aspx" // Web page to handle route
        );
    }