您的位置:首页 > 编程语言 > ASP

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: