in ASP.Net MVC into an existing Web Site Project?

I have a legacy (haha) ASP.Net Webforms Web Site Project in Visual Studio 2008 SP1, that I would like to gradually introduce some MVC functionality into.

Most of the information I can locate on how to integrate ASP.Net MVC with WebForms seems to assume the use of a Web Application Project . However, it seems impossible to find information about how to retrofit an existing ASP.net Web Site Project with the ASP.Net MVC features.

I've reviewed Scott Hanselman's post and Chapter 13 of his upcoming book, both of which assume the Web Application Project type.

Is this possible? Does anyone have a how-to on this?


Well for starters adding MVC to a webforms project is pretty simple, to get the features in VS 2008 for MVC takes a little bit more work (still easy). First you want to be sure you reference the assemblies and are using .Net 3.5. Second you can create a controllers folder and views folder in your current web forms project. You can also create a simple controller with an index action. Then setup/configure the routes in the global.ascx file. You should be set from there. Check here for reference.

However you will only be able to create aspx pages with code behinds (you can delete those and enter the right inheritance class in the markup). To actually "convert" your project type so that you get the goodness of MVC and visual studio (add new view, goto controller, etc) is going to take some playing around with. My best advice is to create a new MVC project in VS 2008 and a new Web App project and compare the .csproj files in plain text. There is a long string value that tells VS the project template.

Believe me this does work. I have done it before on my own legacy projects. I don't remember how I found the project type "key" besides trial/error/elimination. ASP.Net MVC does play nice in the same project as webforms.

UPDATE: I think you can change to an MVC project type, which is still a web application by using these in the PropertyGroup of the .csproj file. Compare those to what you have and change the one that are differnt, be sure to copy/backup the file.

<ProjectGuid>{B99EC98A-1F09-4245-B00D-5AF985190AA9}</ProjectGuid>
<ProjectTypeGuids>{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

Update 2: You wouldn't affect your project or impact it very much. If you are un easy about it make a backup and play around. If you encounter changes you will always have the backup. I was skeptical at first but was glad I went down the MVC path.


I thought I would give an updated answer using Visual Studio 2010 SP1 / NuGet / Scott Hanselman's totally unsupported utility.

  • Install MVC3 (w/ nuget) http://www.asp.net/mvc/mvc3
  • Go to "Tools" menu in Visual Studio and select "Library Package Manager" "Package Manager Console"
  • Once console window appears, change the "Default Project" to be your webforms project.
  • Type "Install-Package AddMvc3ToWebForms" (http://nuget.org/List/Packages/AddMvc3ToWebForms)
  • This will add all necessary dlls, javascript files, web.config setting changes, etc to the project. If everything was successful, you should be able to press F5, navigate to "home" on your website, and see a sample form rendered by mvc: "Welcome to ASP.NET MVC, upgraded with a NuGet package in a totally unsupported way by Hanselman! No warranty!".


    For a WebSite project, you just need to add Controllers to App_Code, not the root. You'll miss some VS goodness - as it doesn't know you're using MVC without the csproj file, but you'll actually get it working.

    Just remember to inherit from Controller and ViewPage and you should be good.

    链接地址: http://www.djcxy.com/p/62440.html

    上一篇: SlowCheetah不在CI服务器上生成转换后的配置文件

    下一篇: 在ASP.Net MVC到现有的网站项目?