Mixing ASP.NET MVC and Webforms
2012-02-23 14:44
741 查看
http://www.aspnetmvcninja.com/general/mixing-asp-net-mvc-and-webforms
Do you have a existing ASP.NET Web Forms project that you would love to migrate to ASP.NET MVC but can’t because you have so much invested in it? I know I did and I’m sure many of you do. The good news is that ASP.NET MVC and ASP.NET Web Forms are compatible allowing you to mix ASP.NET MVC and ASP.NET Webforms in the same project. In this ASP.NET MVC tutorial I’ll show you how to add support for ASP.NET MVC to an existing ASP.NET Web Forms project.
The advantage of adding ASP.NET MVC to an existing ASP.NET Web Forms application is that you can keep your existing code and write all new code using ASP.NET MVC.
So how do you do it? First you need to add references to the following assemblies:
System.Web.Abstractions
System.Web.Extensions
System.Web.Mvc
System.Web.Routing
Once you have added those you need the special ASP.NET MVC folders and you probably want a couple of files that an empty ASP.NET MVC application gets by default. The easiest way to do this is to create an empty ASP.NET MVC project then copy the following folders with their contents and sub folders into your existing ASP.NET Web Forms project
Content
Controllers
Models
Scripts
Views
This gives you all of the special folders and files that you need.
You then need to either create a new Global.asax file or modify your existing one. If you are editing an existing one then you need to add the following to your Global class.
All of that is from the standard Global.asax file for a new ASP.NET MVC 3 application except for the following line which stops routing of any .aspx URL.
You also need to add the following code to your Application_Start() method in Global.
Finally you need to make some changes to your Web.config. As with the previous changes it’s best to copy the missing settings from an empty ASP.NET MVC application. In my case I had to add the following to support ASP.NET MVC 3 with the ASPX view engine.
You can now create a test controller to make sure that you’ve got everything setup correctly. I used the following:
Now run your application and go to /hello. If it works you should see the response Hello World!!
UPDATE 2011-02-28:
After making these changes Visual Studio still didn’t treat the ASP.NET Web Forms project as a ASP.NET MVC project. To get Visual Studio 2010 to treat the ASP.NET Web Forms project like an ASP.NET MVC project I needed to add a GUID to ProjectTypeGuids in my projects .web file. The exact GUID depends on the version of ASP.NET MVC you are using.
ASP.NET MVC 3 – {E53F8FEA-EAE0-44A6-8774-FFD645390401}
ASP.NET MVC 2 – {F85E285D-A4E0-4152-9332-AB1D724D3325}
For ASP.NET MVC 3 my file has the line:
Once that was done Visual Studio behaved properly and displayed all of the ASP.NET MVC specific items in its context menus.
PS: If anyone has the GUID for ASP.NET MVC 1 then please send it to me and I’ll add it above.
Do you have a existing ASP.NET Web Forms project that you would love to migrate to ASP.NET MVC but can’t because you have so much invested in it? I know I did and I’m sure many of you do. The good news is that ASP.NET MVC and ASP.NET Web Forms are compatible allowing you to mix ASP.NET MVC and ASP.NET Webforms in the same project. In this ASP.NET MVC tutorial I’ll show you how to add support for ASP.NET MVC to an existing ASP.NET Web Forms project.
The advantage of adding ASP.NET MVC to an existing ASP.NET Web Forms application is that you can keep your existing code and write all new code using ASP.NET MVC.
So how do you do it? First you need to add references to the following assemblies:
System.Web.Abstractions
System.Web.Extensions
System.Web.Mvc
System.Web.Routing
Once you have added those you need the special ASP.NET MVC folders and you probably want a couple of files that an empty ASP.NET MVC application gets by default. The easiest way to do this is to create an empty ASP.NET MVC project then copy the following folders with their contents and sub folders into your existing ASP.NET Web Forms project
Content
Controllers
Models
Scripts
Views
This gives you all of the special folders and files that you need.
You then need to either create a new Global.asax file or modify your existing one. If you are editing an existing one then you need to add the following to your Global class.
UPDATE 2011-02-28:
After making these changes Visual Studio still didn’t treat the ASP.NET Web Forms project as a ASP.NET MVC project. To get Visual Studio 2010 to treat the ASP.NET Web Forms project like an ASP.NET MVC project I needed to add a GUID to ProjectTypeGuids in my projects .web file. The exact GUID depends on the version of ASP.NET MVC you are using.
ASP.NET MVC 3 – {E53F8FEA-EAE0-44A6-8774-FFD645390401}
ASP.NET MVC 2 – {F85E285D-A4E0-4152-9332-AB1D724D3325}
For ASP.NET MVC 3 my file has the line:
PS: If anyone has the GUID for ASP.NET MVC 1 then please send it to me and I’ll add it above.
相关文章推荐
- Mixing ASP.NET Webforms and ASP.NET MVC
- Plug-In Hybrids: ASP.NET WebForms and ASP.MVC and ASP.NET Dynamic Data Side By Side
- ASP.NET WebForms and MVC together in one project
- Choosing between ASP.NET MVC and ASP.NET Webforms
- Asp.NET MVC and Asp.NET WebForms Features
- 【引】Difference between Asp.Net WebForm and Asp.Net MVC
- Creating a Clean, Minimal-Footprint ASP.NET WebAPI Project with VS 2012 and ASP.NET MVC 4
- How to pass javascript complex object to ASP.NET Web Api and MVC
- 基于 jQuery 的专业 ASP.NET WebForms/MVC 控件库!
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点
- Asp.Net MVC framework And Asp.Net Webform
- Create an Asp.Net Web Forms Application using Bootstrap and Web API
- 在 ASP.NET WebForms/MVC 中利用 HttpModule 添加全局站点统计(CNZZ、百度统计、Google Analytics等)脚本
- Asp.net MVC 4 and .Net 4.5 Bundling and Minification(Web Site Optimization)
- Better, Faster, Easier SSL testing for ASP.NET MVC & WebForms
- Asp.Net Web Forms/MVC/Console App中使用Autofac
- Introducing ASP.NET FriendlyUrls-cleaner URLs,easier Routing, and Mobile Views for ASP.NET Web Forms
- 在 ASP.NET WebForms/MVC 中利用 HttpModule 添加全局站点统计(CNZZ、百度统计、Google Analytics等)脚本
- EF Database First with ASP.NET MVC: Creating the Web Application and Data Models开发遇到问题的解决办法
- ASP.NET vs MVC vs WebForms