How to enable MVC in a WebForms project?

I have an existing webforms project. I’ve 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);

Yet when I navigate to /Test/Test, I get 404 not found. What am I missing?

UPDATE
I ended up adding a few things to my web.config based on the templated MVC5 project and I now have the controller method being hit and the view being located. I’m dealing with something else now but I believe it’s unrelated to MVC configuration, so I think it really is “that easy”.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

After looking at the default MVC5 web.config, adding the following to my config got me up and running:

    <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>

Method 2

Install the mvc package via nuget.

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

Method 3

If you’re using MVC5, the my best suggestion would be to create an entirely new project. In ASP.NET 4.5 and MVC5, they have combined the technologies into what they call One ASP.NET. This makes certain things easier.

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

Then copy all your existing webforms code from the old project to the new. Follow this walkthrough.

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

If, for whatever reason you are unable to do it this way, then a more involved process would be to create the new project, then compare it to your existing project and figure out which web.config entries you need (bear in mind that there’s also a web.config in the Views folder), and which nuget packages need to be installed. I would highly suggest the first approach though, as the second is much more involved.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x