How to integrate asp.net mvc into a website project

As I mentioned in the title, I have a huge WEB SΔ°TE PROJECT and I want to add MVCinto.

I followed some tutorials, but they all focus on integrating MVC into a web application project.

http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx

But I do not have a WEP APPLICATION project.

Is there any way for my problem?

+6
source share
3 answers

There are many blog posts on how to get MVC to work with ASP.NET web applications. However, there are still scenarios in which we use regular ASP.NET website projects, rather than web application projects.

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

1. Install ASP.NET MVC 3

2. Modify web.config

Open 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 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 a controller

Since this is a website project, compilation is performed at run time, so you will have to create your controllers inside the App_Code folder, and not in the regular Controller folder on the main site

Note that your controller class must end with the keyword Controller. In the example with the controller = "Home", the class name for the controller should be HomeController

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

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

enter image description here

5. Check the site

Now that you have created the routing and created the controller, go to localhost / home. You should see "Hello World"

The above content is taken from here . Failed to add the link directly, because the link may be broken.

Hope this helps you

+9
source

Converting an entire website to a web application (WAP) is likely to be painful, so I am not suggesting it. You can try another suggestion from @Grievoushead about getting MVC to work in a website project (which might work), but I suggest an alternative.

Instead of trying to β€œmerge” the website and MVC WAP, try to keep them mostly separate, but share 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 to your WebSite. You may need to merge compartment files such as web.config and global.asax. - Then you want to work on your MVC code, you open csproj in VS. But if you want to work with the source code, just open the folder as a website.

A little unusual, but depending on the specific situation, this may be a good approach.

+3
source

I am afraid that you will have to convert the website into a web application first. See this link for an explanation of how to do this. It may take some effort, but I think the web application format is much easier to work with, so it is definitely worth the effort.

+1
source

Source: https://habr.com/ru/post/949814/


All Articles