Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use ASP.NET MVC together with regular ASP.NET Web forms

I have on request from a client built a huge site with ASP.NET Web forms. The problem is that I'm finding ASP.NET Web forms to be somewhat unintuitive (my personal taste only). So what I would like to do is use MVC but I can't really expect my client to pay for my time on a complete rewrite (nor do he have the time).

So what I'm asking is, can I use ASP.NET MVC and Web forms at the same time and gradually use MVC more and more? Any tips?

like image 833
Daniel O Avatar asked Feb 12 '09 15:02

Daniel O


People also ask

Can you mix MVC and Web Forms?

Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET. There's actually only one crucial difference: ASP.NET lives in System.

Can MVC and ASP.NET coexist?

Yes. MVC is just a different implementation of the IHttpHandler interface so both classic ASP.NET and ASP.NET MVC pages can coexist in the same app.

When should you use .NET Web Forms Over ASP.NET MVC?

Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access. Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development. Hence it is best for developing an interactive web application with the latest web standards.

Which is better Web Forms or MVC?

More Control-The ASP.NET MVC framework provides more control over the HTML , JavaScript and CSS than the traditional Web Forms. Testability-ASP.NET MVC framework provides better testability of the Web Application and good support for the test driven development too.


1 Answers

Update 2014:

Visual Studio 2013 brings us closer to One ASP.NET. There’s no MVC project type or Web Forms project any longer, there’s just ASP.NET. If you want to mix Web Forms and Web API, or MVC and SignalR, go ahead! You are encouraged and supported. New features and functionality are brought in with NuGet without breaking existing apps.

VS 13

So the mixing of Web Forms and MVC (which has been pretty much been working seamlessly) is now encouraged and the boundaries are obfuscated. I guess MS recognized the need to allow projects to slowly migrate to MVC, or just migrate portions as needed, and gain the best of both worlds.


MVC Project:

Having used MVC with Web forms for quite some time it is certainly possible and an excellent choice. New functionality and pages can be easily added into MVC pages with good architectural design such as DDD (Service Repository, Dependency Injection etc.) and the old stuff can stay as it is. Combining MVC inside a webforms page also works fine, although there can be some minor issues with JS validation. I would highly recommend it.

Its fairly easy to start with creating an MVC application (5 atm) and then after you get the basic template up and running add the old webforms inside a folder. This way you get the new MVC setup correctly and it retains backwards compatibility.

  • You can create a MVC project and just copy all the Webforms pages into e.g. a folder. (possible to just upgrade a project too, I'd imagine)
  • Setup routing to ignore requests with that particular foldername in the URL. This way MVC and webforms can be used together without hassle in the URLs.
  • Asp.net WebForms aspx pages can reside within MVC as long as they're not placed inside the /Views folder.
  • Go with Razor over the old syntax, you can customize HTML helpers easily and it's a lot cleaner.
  • Check into MVC standards and conventions, the controller should be very lightweight. It's not the code-behind which can contain as much logic as you need.
  • Views shouldn't contain more than presentation logic (no business logic)
  • Everything new => MVC
  • Resources, web.config etc. are usable by both
  • Menus don't use the old sitemaps in MVC

Using MVC inside a webforms .aspx page:

The text below is meant to demonstrate how you can utilize MVC from inside a webforms page. (For example inside MyPage.aspx) You can use MVC actions/views inside webforms with e.g. ajax to fill a portion of the page or certain div's.

Webforms containing MVC works fine, at least when adding an ajax call inside the HTML to fill a div from MVC.

Inside an .aspx

..html & webforms code

<div id="fillMeFromMVC">
  <script type="text/javascript">
   $.ajax(... call an MVC action to fill 
      the "fillMeFromMVC" div that this script sits inside of);
 </script>
</div>

This will fill a portion of the page via MVC and you can cleanly do your MVC without worrying about what's done in webforms.

WebForms vs. MVC:

By this point you are likely pretty aware of the differences between the two technologies, however here is a little comparison between them. They both have their purposes and uses. I personally prefer MVC for the things I have needed to do, however it likely depends on what you are trying to achieve.

If you use e.g. a SOA behind them both the webforms and MVC pages can utilize the same business logic. Webforms can be detrimental when the code is all in the page behind and is tied to the UI (no separation of concerns). With a solid architecture and a bit of effort that can be reduced though.

Further reading:

Webforms vs. MVC (Code project)

Difference betweeen ASP.NET WebForms and ASP.NET MVC (a blog)

like image 178
lko Avatar answered Oct 01 '22 16:10

lko