How would you sprinkle ASP.Net MVC into an existing website project?

In Visual Studio 2008 Service Pack 1 (SP1), I have an ASP.NET Webforms (haha) project for ASP.NET websites that I would like to gradually implement some MVC features.

Most of the information I can find on how to integrate ASP.Net MVC with WebForms seems to involve using a web application project . However, it seems impossible to find information on how to modify an existing ASP.net website project using ASP.Net MVC functions.

I reviewed Scott Hanzelman's post and Chapter 13 of his future book, both of which suggest a web application design type.

Is it possible? Does anyone have a right to this?

+46
visual-studio asp.net-mvc web-site-project
Feb 20 '09 at 21:35
source share
8 answers

Good for starters adding MVC to their webforms project, it's pretty simple to get the features in VS 2008 for MVC, it takes a bit more work (still easy). First, you want to make sure that you reference assemblies and use .Net 3.5. Secondly, you can create a controller folder and view the folder in your current web form project. You can also create a simple controller with an index action. Then configure / configure routes in the global.ascx file. You must be installed from there. Check here for reference.

However, you can create aspx pages with code entries (you can delete them and enter the correct inheritance class in the markup). To actually "transform" your type of project so that you get the goodness of MVC and visual studio (add a new view, goto-controller, etc.), you need to play a little. My best advice is to create a new MVC project in VS 2008 and a new web application project and compare .csproj files as plain text. There is a long string value that indicates the VS project template.

Believe me, it works. I have done this before in my own projects. I donโ€™t remember how I found the project type โ€œkeyโ€ besides trial / error / elimination. ASP.Net MVC works fine in the same project as web forms.

UPDATE: I think you can switch to the MVC project type, which is still a web application using them in the PropertyGroup of the .csproj file. Compare them with what you have and change those that are different, be sure to copy / back up 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 would not affect the project and would not affect it. If itโ€™s not easy for you to backup and play. If you encounter changes, you will always have a backup. At first I was skeptical, but was glad that I took the path of MVC.

+24
Feb 20 '09 at 22:53
source share

I thought I would give an updated answer using Visual Studio 2010 SP1 / NuGet / Scott Hanselman a completely unsupported utility.

This will add all the necessary DLLs, javascript files, change web.config settings, etc. into the project. If everything was successful, you should press F5, go to "home" on your website and see a sample form submitted by mvc: "Welcome to ASP.NET MVC, completely updated with NuGet package completely without Hanselman support! No guarantee!" .

+19
Apr 08 '11 at 15:41
source share

For a WebSite project, you just need to add the controllers in the App_Code, and not in the root directory. You will miss some VS Q-factor - as it does not know that you are using MVC without the csproj file, but you really will get it working.

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

+7
Jan 26 '10 at 15:06
source share

I had a rather large ASP.NET website (and not a web application) and I wanted to add MVC3 to it. I did not have the opportunity to change the type of project, so I had to go with a website (asp.net 4.0).

I am using a separate MVC project, but not as my own web application, but as an assembly on my old website.

Here is a summary of what I did:

  • I created a new MVC3 web application in Visual Studio 2010, I used the empty template and Razor viewer.
  • I added it to the solution using my existing website.
  • I changed the output path for the assembly from the local bin directory to the bin directory of my website.
  • I removed the Content and Scripts folders from the MVC application. Both content and scripts are already part of my website, and I can also link to them on MVC pages.
  • I deleted the Global.asax files. * from the project. I am using Global.asax on a website.
  • I copied the Views folder and subfolders to the website. Because these are actual files, not part of the assembly, they must exist on the website and not the project that creates the MVC assembly.
  • At this point, I could delete the Views folder from the MVC project, but only in this type of project do I get Visual Studio support to add a new view. Therefore, I sometimes create a presentation here, and then transfer it to the website. When editing cshtml files on my website, I still get the full Intellisense.
  • Added routing to website. I just copied some code from MVC global.asax to global.asax of my website. We need some operations:

     using System.Web.Mvc; using System.Web.Routing; 

In Application_Start, we need:

  AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); 

Then add the usual routing methods:

  public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // your routes } 

Then add a few things to the web.config of your website. In system.web, when compiling, we need the following assemblies:

  <assemblies> <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies> 

Initially, I also added some MVC namespace to web.config, but it seems to work just fine without them.

Now you create new routes in the Global.asax website, then add the appropriate controller to the MVC project, and then return to the website to add a view for it. So, you are all logical in the assembly, while the views and routing are defined on the website.

You can still debug MVC controllers by setting breakpoints there, but you are debugging by running the website.

If you are using the suggested default MVC route:

  routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 

a call to www.mysite.com will serve the contents of the home controller / view, not your old default.aspx, so I just don't use that route. If you are redirecting a conflict with existing physical folders and files, use regex constraints with routes to prevent such conflicts.

Although I use the master pages on the website, the actual html for the common parts of the page is generated by code in another assembly. I could just call the same methods from my _ViewStart.cshtml or my base controller.

So far, I have not seen any real negative consequences of this approach.

+6
Jan 17 2018-12-12T00:
source share

As long as you configure routing in web.config, configure the necessary directory structure and add the correct routes to global.asax, theoretically you can add MVC elements to any web project. As far as I know, these are the only requirements for his work.

However, the combination of the two can be a bit confusing and difficult to maintain in the long run. Perhaps you could move all existing web form content to a subfolder to prevent this, and keep the siteโ€™s root directory clean to reduce clutter and make things clearer.

+2
Feb 20 '09 at 22:53
source share

If you want to add MVC 3 to asp.net, and not to a web project, then the Scott Hanselman AddMvc3ToWebForms nuget package will give you 99% of the way there, but it will cause an error during installation that you can ignore (I think at least in my tests this seems like a case), and after installation it will take a few simple steps.

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

Mark (@delradie)

+1
Aug 04 2018-11-11T00:
source share

Something I learn when trying to port an MVC2 application is that your project needs Default.aspx. I was tasked with adding some GUI functions to an existing web services project, and therefore there was no default.aspx. It took a while to understand why my routes were not configured.

0
Jul 19 '10 at 11:57
source share

The Microsoft.NET 4.0 Web Development Exam (70-519) has almost this exact question in the preparatory materials. The answer, according to Microsoft, is this:

  • Converting web forms into a web application (i.e. webapp project).
  • Add links to "ASP.NET MVC 2 assemblies" in the webapp configuration file.

This information is in the paid materials that my employer acquired, so there is no need for a web page that clearly indicates what I could link to.

0
Jul 27 '11 at 13:16
source share



All Articles