How do I enable MVC in a WebForms project?

I have an existing webforms project. I added MVC5 from Nuget and added the following:

_Viewstart.cshtml
Shared
   _Layout.cshtml
Areas
   Test
      Controllers
        TestController.cs
      Views
        Test.cshtml

      

My controller:

     public class TestController : Controller
    {
    [Route("Test/Test")]
    public ActionResult Test()
    {
        return View();
    }
    }

      

My Global.asax

:

        AreaRegistration.RegisterAllAreas();
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        RouteConfig.RegisterRoutes(RouteTable.Routes); 

      

But when I go to /Test/Test

, I don't get a 404. What am I missing?

UPDATE I ended up adding a few things to my web.config based on the MVC5 templated project, and now I have a control method that gets hit and a view resides. Now I am dealing with something else, but I believe it is not related to MVC configuration, so I believe it really is that simple. "

+3


source to share


3 answers


After looking at MVC5 web.config by default added the following to my config:

    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

      



AND:

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
  </dependentAssembly>

      

+3


source


If you are using MVC5 my best suggestion would be to create a completely new project. In ASP.NET 4.5 and MVC5, they merged technologies into what they call one ASP.NET. This makes things easier.

Create a new ASP.NET WebForms application, then check the MVC checkbox and possibly the WebApi checkbox if you also need WebApi (or not if you don't).

Then copy all the existing web forms code from the old project to the new one. Follow this step by step guide.



https://docs.microsoft.com/en-us/aspnet/visual-studio/overview/2013/one-aspnet-integrating-aspnet-web-forms-mvc-and-web-api

If for some reason you cannot do it this way, the more complicated process would be creating a new project and then comparing it to your existing project and figuring out which entries in the web.config you need (bear in mind that there is also a web .config in the Views folder) and which nuget packages need to be installed. I would really like to suggest the first approach, since the second is much more active.

0


source


Install the mvc package via nuget.

https://www.nuget.org/packages/Microsoft.AspNet.Mvc/

0


source







All Articles