Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can MVC framework be used in a web application that is currently using ASP .NET?

I recently joined a group that manages a Classic ASP web application. It has been working fine for our group's need. However, a decision was made, before I joined, to move to ASP .Net. Since we are mostly ASP developers, we write code in ASP .Net as we would in Classic ASP (for the most part). Would it be possible to introduce MVC to this application/project?

Thanks!

like image 553
chaltahai Avatar asked Oct 09 '12 19:10

chaltahai


3 Answers

Yes, it is possible to use MVC in a traditional WebForms project. I migrated a large WebForms project to MVC 2 a couple of years ago, and here are my findings (I have updated them to reflect MVC 3)

  1. Make sure you have .NET 4.0 installed, as well as the MVC 3 framework and VS extensions.
  2. Create a new blank MVC project to use as a reference.
  3. Look at the default web.config for the reference project. You basically want to use the reference web.config, and merge in stuff you need from your current project.
  4. Look at the reference global.asax.cs. Similar to the above, you want to merge the changes in the reference .cs into your current application's global.asax.cs.
  5. You will need to add the following references to your web project:

    System.Web.Abstractions, System.Web.Extensions, System.Web.Helpers, System.Web.Mvc, System.Web.Routing

  6. You can enable the VS extensions by changing the ProjectTypeGuids:

    • In Solution Explorer, right-click the project name and select Unload Project. Then right-click the project name again and select Edit ProjectName.csproj.
    • Locate the ProjectTypeGuids element and add {E53F8FEA-EAE0-44A6-8774-FFD645390401}.
    • Save the changes, right-click the project, and then select Reload Project.
  7. Add the following standard folders for MVC content:

    • ~/Views
    • ~/Views/Shared
    • ~/Controllers
    • ~/Models (for your view models, optional)
    • ~/Content (for CSS and images, optional)
    • ~/Scripts (for JS, optional)

Additional notes:

  1. If your existing WebForms relies on web.config settings for authorization (such as preventing unauthorized users), this won't be recognized by MVC actions, because routing works completely separately from the WebForms authorization. Use AuthorizeAttribute to require authorization, or constrain actions to certain roles or users. You can even specify global filters so you don't have to apply this attribute on every single controller or action.
  2. There may be additional considerations for making MVC work with IIS versions prior to 7, or with application pools that use the Classic pipeline. Consider using IIS 7+ with Integrated pipeline.
  3. My notes above mainly involve getting the baseline of MVC working, which uses ASPX views. ASPX views use the same markup as the ASPX files you are used to in WebForms. You can also use the new Razor syntax (primer), which I highly recommend. You can use both ASPX and Razor view pages at the same time. However, you cannot use an ASPX master page on a Razor view (or vice versa). Also, MVC will find and use ASPX views before Razor views, so if you upgrade a view to Razor, delete the original ASPX. You will need to do a little additional work to enable Razor views. I'm trying to find my notes for enabling Razor. I'll update when I find them. Once you do have Razor installed and working, you can use this tool by Telerik to convert ASPX to Razor.
  4. Here is a question on SO about a problem I'd had while upgrading. I'm only providing it because it covered some of the points I mentioned above in more detail. However, I was upgrading to MVC 2 at the time, so some of this stuff is out of date.
like image 120
moribvndvs Avatar answered Sep 30 '22 02:09

moribvndvs


The short answer is yes it is possible.

Scott Hanselman has written about this topic before.

Depending on the skills in your team, you may well find it difficult to get up to speed - it is possible to write clean MVC style code in Classic ASP but most people don't.

like image 29
cspolton Avatar answered Sep 30 '22 04:09

cspolton


It is of course technically possible. However, it sounds like it would be a cultural shock, as MVC works quite a bit differently than Classic ASP or ASP.NET WebForms. I think it would be worthwhile to do - or just stick with ASP.NET WebForms if that seems more natural. But if you've not yet fully committed to WebForms, MVC seems just as easy to move to, IMO.

like image 29
Andrew Barber Avatar answered Sep 30 '22 02:09

Andrew Barber