31

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?

1 Answer 1

17

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)

5
  • I think you're wrong because MVC RouteCollection has this setting called RouteExistingFiles which is false by default. This means that if you access an existing aspx page (and it doesn't reside inside the Views folder (local web.config prevents direct request access to files), it should be handled as an everyday WebForm. May 9, 2012 at 8:40
  • Ok, I'm not entirely sure about that point, I edited the post.
    – lko
    May 10, 2012 at 9:45
  • @Iko: if you'r enot sure, you can test it, right? :) But I wasn't talking about embedding Web forms server controls inside MVC view... I was saying that web forms pages can reside inside MVC web applications. So you obviously misunderstood my comment because what you were doing is mixing WebForms page with partial Asp.net MVC view. And that's a different thing that OP was asking about. May 10, 2012 at 17:38
  • @RobertKoritnik Ok good to know, so you can do the reverse (Webforms inside a MVC page) . However he was asking about using them both in the same project. I can assume that at some point he may want to update a portion of a page, in which case it is simple to just drop the webforms section and replace it with MVC. Let's say there's some bloated complicated control which he could replace with MVC, this is possible using my suggested solution. He asked for tips and I've done this before and I see it as a likely situation he could encounter (especially as he notes his preference is MVC).
    – lko
    Oct 19, 2012 at 7:35
  • Placing the webforms in a folder of an MVC project sounds like a clean solution but I fail to understand how it would work in terms of shared assets such as scrips, images, fonts, etc... Also, it's not clear in your explanation how to navigate to the webforms without having its containing folder in the URL. Would you be able to kindly explain?
    – Organic
    Jun 9, 2016 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.