12

I'd like to add MVC support to an existing Website project (not a Web application project) and have a few questions.

  1. To get Visual Studio's MVC goodness, it appears that I need to update the .csproj file's <ProjectTypeGuids> node. There is no csproj file in a website project.
  2. It appears that I need to create Models and Controllers folder in the App_Code. Is this true? I prefer having these folders under root.
  3. For Views should I be creating a aspx and aspx.cs files? Is cshtml razor files supported in this kind of a setup?

Any other responses are appreciated. Thanks

Nick
  • 7,475
  • 18
  • 77
  • 128

6 Answers6

9

With asp.net MVC2 and above, the MVC team separated the core functionality into three different assemblies, each of which extends from the common System.Web assembly:

  • System.Web.Routing
  • System.Web.Abstractions
  • System.Web.Mvc

With this seperation, they went ahead and made the assemblies to "work in Medium-trust server enviroments and be bin-deployable".

One of the good things about this featuere is, you don't have to have a specific project type to run MVC. You only need the assemblies, some directories and a tweaked web.config.

To do this, you need only to place the assemblies in your local bin folder of your project and make the necessary references for those assemblies. Once this is done, you have access to asp.net MVC.

Here are some detailed instructions from the Wrox Professional ASP.NET MVC 1.0 book which should help you get started:

Including MVC in Existing Web Forms Applications

Adding ASP.NET MVC functionality to an existing Web Forms application is comprised of three different steps:

1. Add a reference to the three core libraries that ASP.NET MVC needs: System.Web.Mvc, System.Web.Routing, and System.Web.Abstractions.

2. Add two directories to your application: Controllers and Views.

3. Update the Web.config to load the three assemblies at run time as well as registering the UrlRoutingModule HttpModule.

For reference, here are a couple of blogs/sites which have some more detailed scenarios which might help you out:

Mixing ASP.NET Webforms and ASP.NET MVC

ASP.NET WebForms and ASP.NET MVC in Harmony

Good luck, and hope this helps you out some.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
Chris
  • 6,272
  • 9
  • 35
  • 57
  • I do have MVC added to the project without any issues. However, my question is regarding getting Visual studio support to this hybrid project. It appears that this can be done in the web app project by modifying the `csproj` file but i haven't figured out how to get it working with a website project. I also have other questions that were outlined earlier. – Nick Aug 30 '11 at 14:25
  • Razor should work fine, but just for keeping things looking all the same since you're already working with .aspx files, I would go ahead and keep the MVC views with the webform view engine. This is probably more of a call of yours. For using the App_Code directory, if you're using a straight up website, you probably do have to use it for the models and controllers. If you've gone ahead and changed into a web application, everything gets complied down to a single DLL, so it really doesn't matter where those folders go. – Chris Aug 30 '11 at 14:53
  • Since the controllers folder needs to be in `App_Code`, is there a way to add a class library and reference to the website project? I'd like to stay away from adding anything in the `App_Code` folder if possible. Also, any idea how I'd integrate the use of `Areas`? – Nick Aug 30 '11 at 18:08
  • Since with MVC you're just calling methods and not pages, having a class library should work just fine. Now area's I'm not sure. I don't know how big your project is, or where you're trying to take the code base to. As an off the cuff answer, if you can do MVC now, you should be able to add in areas also, but I have no reference for you on this. – Chris Aug 30 '11 at 20:31
  • This is a rather large project (1000s of pages, 100s of projects). The problem I see with adding areas is that areas expect its own `web.config` and the `AreaRegistration` class file. Since I am separating this out to a different class library, it doesn't like the `web.config`. Also, with a separate class library, I'd have to reference the Session object like the way I do in the controllers. My understanding is that we should not use `HttpContext` within a class library and it's not a good practice. Thoughts? – Nick Aug 31 '11 at 13:04
8

I've successfully added a ASP.NET MVC 3 to Webforms project and here are some suggestions:

  • Add Controllers as a separate class library project and add a reference to this project from the Web forms web project
  • I attempted to get VS MVC support (like goto controller etc), but adding the GUID {E53F8FEA-EAE0-44A6-8774-FFD645390401} to the .csproj file didn't help.
  • Yes, you can add references to get the Session from your class library. You can always mock it out if you want to write unit tests.
  • Add Views folder to the root
  • Add Models within App_Code
  • If you are using Razor, then you need to add System.Web.Razor and System.Web.WebPages references
  • If you are using Razor, update the Web.config per this post
  • Keep in mind, you can add server controls to your view as long as they don't use postbacks (I had a lot of server controls from my webforms project that I had to use)
Community
  • 1
  • 1
DotnetDude
  • 11,617
  • 35
  • 100
  • 158
  • lookout with the use of vitual paths. On my website when I tried to add mvc on it didn't work after this steps only because I was using '.' on the virtual path (E.g : "project.web") . – hopper Mar 27 '12 at 15:46
1

I believe if you set up a new MVC project and copy your web forms across to the new project, they will render as expected.

I haven't experimented with this too much but I have tried in the past out of curiosity and the web forms were still rendered OK. I guess it depends on the complexity of your project as to whether this approach would work.

This would involve changing your project type.

TheCodeKing
  • 19,064
  • 3
  • 47
  • 70
  • The existing project is indeed very complex. I am okay with adding the MVC project to it, but would stay away from creating a new MVC project and copy the webforms project to it unless I really really need to. Do you have a resource that may help if I end up going in that direction? – Nick Aug 30 '11 at 14:07
0

I was tasked with updating (but unfortunately not re-writing) a legacy .Net 3.5, VB.NET (ughh) webforms, web site project and I successfully upgraded to 4.0 and added MVC3 support (with seperate code compliation folders to support C#, yah!), and it works just fine.

The @DotnetDude instructions do work, but be careful of a couple of things...

(1) When adding Razor support, this is done in the Views/Web.config file not the web.config file in the root of your project.

(2) If you do happen to add a Razor file (.chtml or .vbhtml) OUTSIDE of the Views directory, vs.net will update your root web.config with the following value

<appSettings>
  <add key="webpages:Enabled" value="true" />
</appSettings>

Not good. This setting is to allow direct browsing of razor pages and when set to TRUE in my case, caused everything to break. That being said, I'm only using razor pages in my Views subfolder, however what I found nice is making AJAX calls from my .aspx pages to a controller defined in the App_Code directory allowing me to modernize an app that was mostly all postbacks, and C# to access the VB.NET written data layer.

AgonyOfVictory
  • 164
  • 1
  • 4
0

I have seen this work in the past if you place a Global.asax file in the root of your website. You'll need a way for your project to recognize and differentiate MVC requests from standard requests, like this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute("Default",
        "{controller}.mvc/{action}/{id}",
        new { controller = "Home", action = "Index", id = "" }
    );
}

So, an MVC url in your app might look like: http://www.mywebsite.com/mycontroller.mvc/View/5

WEFX
  • 8,298
  • 8
  • 66
  • 102
  • Yes, that is possible. However, I am curious to know if I can get VS MVC goodness in a website project (and other questions outlined earlier) – Nick Aug 30 '11 at 14:08
  • You don't have to differentiate between mvc & webform requests. The routing will automatically work out which one to show. If the urls conflict, then you may need to change things to work. – Simon Halsey Aug 31 '11 at 23:43
-1

ASP.NET MVC does not support web site template, it can only be Web Application. So you can not add MVC functionality to Web Forms project.

  • 4
    That's just not true. You can add MVC to a web forms project. The VS tooling may not work as well, but MVC will work just fine if you follow the trivial steps outlined by @chris – Simon Halsey Aug 31 '11 at 23:39