Search code examples
c#asp.netasp.net-mvcwebformsasp.net-mvc-routing

After adding MVC support to a WebForms project, aspx pages don´t load


I´m starting working in a web forms project done on .net framework 4. We want to start working new features in MVC and start migrating slowly the old legacy features, and we wnat to tak advantage of the capability of having MVC alongside aspx. This was initially and asp.net web forms project.

I started by adding MVC. since the project targets framework 4, I installed MVC 4.0.40804. Upgrading framework at this time could be cumbersome and require a lot of code to be refactored.

So far, the installation of components was good and the project sompiles. I started it to check the aspx files still worked. and Boom. they don´t. I get a 404 error. the default page is Account/Login.aspx but it is changed to Account/Login?ReturnUrl=%2fAccount%2fLogin.aspx.

enter image description here

So I started checking and found this article from Scott Hanselman and tried configuring my RouteConfig file, but nothing worked. Right now my file looks like this.

    public static void RegisterRoutes(RouteCollection routes)
    {
        //THIS IS ADDED BY VS
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
        //THIS I ADDED AT FIRST UPDATE
        routes.IgnoreRoute("Account/{myWebForms}.aspx/{*pathInfo}");
        routes.IgnoreRoute("{myWebForms}.aspx/{*pathInfo}");
        //THIS I ADDED SECOND
        routes.IgnoreRoute("Account/{*pathInfo}");
        //THEN I ADDED THIS
        routes.IgnoreRoute("Account/Login.aspx");

        //THEN I ADDED THIS IN THE FINAL ATTEMPT
        routes.MapPageRoute(
            "login",
            "Account/{myWebForm}",
            "~/Account/Login.aspx"
            );

        //THIS IS DEFAULT CONFIG AND WAS HERE FROM THE BEGGINING
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new
            {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            }
        );
    }

None of the attempts changed the behaviour, with any configuration it loaded the account.Login.aspx webForm as intended and the same error abve was shown each time I run in debug.

How can I configure it?

another thing, I guided myself with the MVC Movies for MVC 4 starting guide code. so check on versions and file. I did not create the bundleConfig or webapiconfig classes yet, only RouteConfig.

Update Here is the and tags from the web config file. besides these tags, there are also configurations for connection strings, application settings and WCF services settings that are not included.

At the end there is a tag with a binding to Newtonsoft.json dependency.

  <system.web>
    <httpRuntime requestValidationMode="2.0"/>
    <compilation debug="true" targetFramework="4.0"/>

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
    </authentication>

    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>

    <!-- <sessionState cookieless="AutoDetect" mode="StateServer" /> -->
  </system.web>

Solution

  • Despite being routed to login.aspx, the authentication will be based on the URL. It just looks like forms authentication is not allowing you to Access Acount/Login without being authenticated.

    Try adding the following to your web.config:

    <configuration>
        <location path="Account/Login">
            <system.web>
              <authorization>
                <allow users="*" />
              </authorization>
            </system.web>
          </location>
    </configuration>
    

    That might work.

    EDIT:

    By way of an explanation, the * in the tag just means to let anyone access this page, regardless of authentication status. If you're adding any other routes for pre-existing web form pages you may also need to add tags for these if they're supposed to be anonymous.

    If you add any MVC pages (.cshtml) to your application, you should be able to use the [Authorize] and [AllowAnonymous] attributes on your actions to control authentication on those, but custom routes for the web forms will need location tags in your web config.