Migrating from ASP.NET WebForms to ASP.NET MVC - asp.net

I'm developing a web application for a company which I work for. My team started working on the app few months ago and the decision was to build it with ASP.NET WebForms. Now we've quite a lot of the code developed and we're wondering if ASP.NET WebForms was a good choice. Maybe we should migrate. Ok, but what's the first step? We don't want to rewrite everything from scratch. We'd like to add a new stuff in MVC and rewrite the old part in the future (gradually). Is it possible to add somehow ASP.NET MVC application to current WebForms one? Can they live together?

Asp.net webforms and MVC can live happily together. You will add some includes and directores and add a route which will cause your webforms pages to be ignored. All explained here:
http://www.packtpub.com/article/mixing-asp.net-webforms-and-asp.net-mvc

Mixing MVC with webforms is not that all hard. Basically, you want to ignore any exisiting .aspx routes in your global.asax, and then add routes for new pages that you want to build using MVC.
See this article for more details.

Related

How to create MVC4 wrapper around existing/legacy classic ASP application?

I have a large legacy web application written in a combination of classic asp and asp.net webforms. The client has agreed to rewrite the application and we have decided to use MVC4 but of course we don't have the time to stop support on the legacy application to build the new application.
Is there a way to put an new MVC4 wrapper application around the existing/legacy application so that parts of the legacy application can be ported to the new environment piece by piece?
this is pretty old question, but I ran across it so thought I'd try and help out. You can still use the .aspx pages within MVC4 by modifying the routing functionality of MVC. this link should help you get started: http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx

mix-up WebForm and MVC in one project vs separate them in 2 projects in the same solution

I've tried to mix up WebForm and MVC in the same application, but so far I've failed miserably. It looks like I'm missing some steps some how some where. I'm really tired.
I wonder if just it's bad prectice to have 3 projects in a solution: The first one for the Model, the second one for the Webform, and the last one for the MVC.
Thanks for helping
If you have two separate projects one for MVC and one for classic WebForms it's like you have two distinct web applications. Those two should be deployed separately in different virtual folders in IIS.
On the other hand you have the possibility to mix classic WebForms and ASP.NET MVC in the same project.
Usually people have some legacy WebForms application that they want to migrate in ASP.NET MVC. But due to the sheer amount of code this cannot be done at once so you would create a new ASP.NET MVC application and import the existing legacy WebForms inside it which could be directly used. Then you could progressively update legacy code to the MVC pattern.
But from personal experience I find it dirty mixing classic WebForms with ASP.NET MVC. My hands just feel dirty. What I do is that I would keep legacy WebForms as a separate application and start replacing different sections of it with a new ASP.NET MVC application and the two of them would communicate only through standard HTTP techniques (usually GET and POST verbs).

Ideas to improve/enrich a WebForms application by adding ASP.NET MVC elements to it

I'm currently present with the following situation.
We have a huge enterprise application written with WebForms. Refactoring it or completely rewriting it is out of the question. So I'm not talking about migration WebForms -> MVC.
However, I understand one can technically add MVC functionality to coexist with the rest of the project. I was asked to present MVC concepts for a team so that we can consider how it could become (or not) of use to us.
After careful thinking I do not see any ways or reasons to add MVC elements to a WebForms project since it will become a strange breed then.
Maybe there are certain not exactly obvious possibilities to add value to a WebForms project with MVC framework? Has anybody thought about it?
Sharing a master between MVC and WebForms - dealing with the
Mixing ASP.NET Webforms and ASP.NET MVC
ASP.NET MVC WebForms Hybrid application
Combining MVC and WebForms from Pro ASP.NET MVC Framework (also available here)
I would also look at this article from Scott Hanselman. He seems to have the mojo on mixing these technologies.
Plug in Hybrids...Mixing ASP.NET and ...

Can "classic" ASP.NET pages and Microsoft MVC coexist in the same web application?

I'm thinking about trying out MVC later today for a new app we're starting up, but I'm curious if it's an all or nothing thing or if I can still party like it's 2006 with viewstate and other crutches at the same time...
Yes you can have your webforms pages and MVC views mixed in a single web application project. This could be useful if you have an application that is already built and you want to migrate your app from webforms to mvc.
You need to make sure that none of your webforms pages go in the 'Views' directory in a standard ASP.NET MVC application though. Pages (or views) in the 'Views' directory can't be requested directly through the url.
If you are starting an application from scratch, there would be very little benefit to mixing the two.
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.
As you've probably noticed with the above answers, yes this is very possible to do.
I've actually had to do this on my current project. I was able to get approval to add MVC to our application, but only in the administration section (to limit the risk of affecting current members coming to our site).
The biggest problem I had was converting my Web Site to a Web Application, but once that was done, things were pretty straight forward adding MVC side-by-side our classic code-behind web pages.
The trick for me was to make my MVC pages look as similar as possible to my code-behind pages so the transition looked as seamless as possible.
I am currently working on a new project. While I would like to go down the MVC route all the way, some of the project requirements don't allow me.
One of those requirements is to have a grouping grid from the client-side. Personally have chosen the Telerik Rad-Grid. While they may be in the process of supporting MVC they are not there as yet.
So this means that I have to have a hybrid solution. for the time being until RadGrid fully supports MVC.
While we are in this transition period I think that there will be may more hybrid projects out there until the support of the Third Party Controls catches up.
Regards
Nathan
You'll need to make sure your MVC routes don't conflict with your Web Forms pages so that requests for a .aspx page don't get routed to a controller action as a parameter etc.
See this blog post by Phil Haack for details on how to avoid this.
Yes, it is very much possible for MVC pages to coexist with asp.net web forms. I implemented that in my existing asp.net application for adding new features. We need to make sure of referring the MVC DLLs, registering routing tables for URL routing and configuring the assemblies and namespaces in Web.config file.
If you're mixing MVC with other methodologies you're not really getting the benefit out of it. The point of MVC is to allow you to decrease coupling and increase cohesion, and if only half of your code is doing that, then the other half is inevitably going to restrain your development cycle.
So, I guess while it's possible, I don't think it's worth it. Go all the way or don't go at all.

Hybrid WebForms/ASP.NET MVC [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
ASP.NET MVC alongside Web Forms in the same web app?
I am kinda new to .NET applications, I have a web forms application that I am working on and I am about to start the development of new pages, I am trying to make the new pages with the ASP.NET MVC, but I am not beeing able to make the pages hit the controllers. First i added the library references added the routes on the global.asax but not sure what else is missing, can someone help me out?
thx.
Reading between the lines I gather you are trying to add MVC pages to your existing ASP.NET Webforms Webapplication?
If that is the case then you probably need some MVC specific config. settings. Easiest way is to create a new MVC web app and then merge the config settings.
Furthermore if you are doing a hybrid project I'd recommend giving Scott Hanselmann's post on the topic a read.
There's a more step-by-step description of adding MVC to a WebForms Application at these links:
Integrating ASP.Net MVC Into An Existing ASP.Net Web Application
Mixing ASP.NET Webforms and ASP.NET MVC
You might also want to add the ProjectTypeGuid to the project file with a text editor ({603c0e0b-db56-11dc-be95-000d561079b0} is used to designate to Visual Studio to use the MVC extensions.)
<ProjectTypeGuids>{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
The links above also don't fully cover the system.webserver area of web.config that needs to be configured.
Scott Hanselman released a "totally unsupported" Nuget package that adds MVC 3 features to an existing Web Forms project. It also works on my PC (ha ha) and I've used it on several projects.
http://nuget.org/List/Packages/AddMvc3ToWebForms
He blogged about it here:
http://www.hanselman.com/blog/CreatingANuGetPackageIn7EasyStepsPlusUsingNuGetToIntegrateASPNETMVC3IntoExistingWebFormsApplications.aspx
One newb tip: this package will configure your MVC routes in /App_Start/RegisterMvc3Routes.cs
I've spent a lot of time over the past few months on this. Here are my observations.
The good/easy
- Getting Webforms to call into MVC controllers
- It was remarkably easy to stand up a new MVC3 project and drop Webforms pages into it.
- I was able to move my <pages><controls></controls></pages> section into the /pages directory in a new web.config there
The dirty/difficult
Regarding the GUID
Please note that the GUID has to be added at the front of the line for some reason... everytime I tried it failed. Until I stumbled on a post that insisted it be the before the others.
also I don't know what the difference is but I have a different GUID working... {E53F8FEA-EAE0-44A6-8774-FFD645390401}
getting the landing page to be Webforms caused ALL kinds of snags.
getting jQuery intellisense to play nicely with T4MVC
this is what I did to address that
#if (System.Diagnostics.Debugger.IsAttached)
{
<script src="../../Scripts/Mvc3/jquery-1.7-vsdoc.js" type="text/javascript"></script> #* intellisense! *#
#Html.RelativeJavascript(Links.Scripts.Mvc3.jquery_1_7_js)
#Html.RelativeJavascript(Links.Scripts.Mvc3.jquery_unobtrusive_ajax_js)
}
else
{
#Html.RelativeJavascript(Links.Scripts.Mvc3.jquery_1_7_min_js)
#Html.RelativeJavascript(Links.Scripts.Mvc3.jquery_unobtrusive_ajax_min_js)
}
Recommendations:
Use T4MVC in ALL cases even if you are pure webforms. The elimination of magic strings for static content (.js,.css, images, specifying templates) is outstanding.
and if you have any part of your build process compiling views then you get compile-time safety on any of those links.

Resources