How would you sprinkle-in ASP.Net MVC into an exis

2019-01-16 04:46发布

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?

8条回答
你好瞎i
2楼-- · 2019-01-16 04:55

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-16 04:56

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.

查看更多
Ridiculous、
4楼-- · 2019-01-16 04:58

If you want to add MVC 3 to an asp.net website rather than web project then Scott Hanselman's AddMvc3ToWebForms nuget package will get you 99% of the way there, but will throw an error during install you can saefly ignore (I think, at least in my tests this seems to be the case), and a couple of simple steps are needed after install.

Full details on http://delradiesdev.blogspot.com/2011/08/adding-mvc-3-to-aspnet-web-site.html

Mark (@delradie)

查看更多
贪生不怕死
5楼-- · 2019-01-16 05:00

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

  1. Install MVC3 (w/ nuget) http://www.asp.net/mvc/mvc3
  2. Go to "Tools" menu in Visual Studio and select "Library Package Manager" \ "Package Manager Console"
  3. Once console window appears, change the "Default Project" to be your webforms project.
  4. 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!".

查看更多
走好不送
6楼-- · 2019-01-16 05:05

Something that I learn, while trying to migrate an MVC2 application is that your project needs a Default.aspx. I was tasked with adding some GUI functions to an existing Web Services project, and therefore there was no default.aspx. Took me a while to figure out why my routes weren't being setup.

查看更多
等我变得足够好
7楼-- · 2019-01-16 05:06

As long as you setup the routing in web.config, setup the necessary directory structure, and add the correct routes in global.asax, you could theoretically add MVC items to any web project. So far as I know, those are the only requirements for it to work.

However, the combination of the two might be a bit confusing and difficult to maintain, long term. Maybe you could move all of the existing web forms site content into a subfolder to keep it out of the way and keep the root directory of the site clean to reduce the clutter and make things more clear.

查看更多
登录 后发表回答