How to integrate asp.net mvc to Web Site Project

There are numerous blog posts on how to get MVC to work with ASP.NET Web Applications. However there are still scenarios where we are using normal ASP.NET website projects rather than Web Application projects.

Below are the steps to enable MVC 3 with an asp.net website project

1. Install ASP.NET MVC 3

2. Modify web.config

Open up web.config in Visual Studio and add the following lines inside the section

enter image description here

3. Modify global.asax

Next you will need to add in the code for MVC triggers inside global.asax (create one if it does not exist)

Add the following lines after <%@ Application Language="C#" %>

enter image description here

Add the following after

enter image description here

add the following inside application_start

enter image description here

At this point, your global.asax should look like

enter image description here

4. Creating the controller

Because this is a website project, compilation is at runtime, so you will have to create your controllers inside the App_Code folder rather than the normal Controller folder in the main site

Note that your controller class needs to end with the Controller keyword. In the example, with a controller = “Home”, the classname for the controller needs to be HomeController

To add your first controller, right click on the App_Code folder and create a new class with the file name as HomeController.cs

Paste the following code into the HomeController.cs (replace everything)

enter image description here

5. Test the site

Now that you have generated the routing and created the controller, browse to localhost/home. You should see “Hello World”

The above contents are taken from here. Could not add the reference directly because the link can be broken.

Hope this should help you


Converting the whole Web Site to a Web App (WAP) is likely to be painful, so I don't suggest it. You can try the other suggestion from @Grievoushead of making MVC work in a Web Site project (which can work), but I'll suggest an alternate one.

Instead of trying to 'merge' the Web Site and MVC WAP, try keeping them mostly separate, but sharing the same folder. You could do something like this:

  • Start by creating an MVC project in a separate folder
  • Copy all the files from it into your WebSite. You may need to hand merge a coupe files like web.config and global.asax.
  • Then it you want to work on your MVC code, you open the csproj in VS. But if you want to work on your original code, just open the folder as a Web Site.

A bit unusual, but depending on the exact situation, this may be a good approach.