如何将asp.net mvc集成到Web站点项目中

时间:2021-04-30 04:02:43

As I mentioned in title, I have a huge WEB SİTE PROJECT, and I want to add MVCinto it.

正如我在标题中提到的,我有一个巨大的WEBSİTE项目,我想将MVC添加到它。

I have followed some tutorials about it but all of them are about integrating MVC into a web application project.

我已经关注了一些关于它的教程,但所有这些都是关于将MVC集成到Web应用程序项目中。

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

But I don't have a WEP APPLICATION PROJECT.

但我没有WEP APPLICATION PROJECT。

Is there any way for my problem too?

我的问题也有办法吗?

3 个解决方案

#1


10  

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.

关于如何让MVC与ASP.NET Web应用程序一起使用,有很多博客文章。但是仍然存在使用普通ASP.NET网站项目而不是Web应用程序项目的情况。

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

以下是使用asp.net网站项目启用MVC 3的步骤

1. Install ASP.NET MVC 3

1.安装ASP.NET MVC 3

2. Modify web.config

2.修改web.config

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

在Visual Studio中打开web.config并在该部分中添加以下行

如何将asp.net mvc集成到Web站点项目中

3. Modify global.asax

3.修改global.asax

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

接下来,您需要在global.asax中添加MVC触发器的代码(如果它不存在则创建一个)

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

在<%@ Application Language =“C#”%>之后添加以下行

如何将asp.net mvc集成到Web站点项目中

Add the following after

之后添加以下内容

如何将asp.net mvc集成到Web站点项目中

add the following inside application_start

在application_start中添加以下内容

如何将asp.net mvc集成到Web站点项目中

At this point, your global.asax should look like

此时,您的global.asax应该是这样的

如何将asp.net mvc集成到Web站点项目中

4. Creating the controller

4.创建控制器

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

因为这是一个网站项目,编译是在运行时,所以你必须在App_Code文件夹中创建控制器而不是主站点中的普通Controller文件夹

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

请注意,您的控制器类需要以Controller关键字结束。在示例中,使用controller =“Home”,控制器的类名必须是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

要添加第一个控制器,请右键单击App_Code文件夹并创建一个文件名为HomeController.cs的新类。

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

将以下代码粘贴到HomeController.cs中(替换所有内容)

如何将asp.net mvc集成到Web站点项目中

5. Test the site

5.测试网站

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

现在您已生成路由并创建了控制器,请浏览到localhost / home。你应该看到“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

希望这对你有帮助

#2


5  

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.

将整个网站转换为Web应用程序(WAP)可能会很痛苦,因此我不建议这样做。您可以尝试@Grievoushead提出的使MVC在Web站点项目中工作的其他建议(可以工作),但我会建议另一个建议。

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.

而不是试图“合并”网站和MVC WAP,尝试保持它们大多是分开的,但共享相同的文件夹。您可以执行以下操作: - 首先在单独的文件夹中创建MVC项目 - 将其中的所有文件复制到您的WebSite中。您可能需要手动合并像web.config和global.asax这样的coupe文件。 - 然后你想要处理你的MVC代码,你在VS中打开csproj。但是,如果您想处理原始代码,只需将该文件夹作为网站打开即可。

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

有点不寻常,但根据具体情况,这可能是一个很好的方法。

#3


1  

I'm afraid that you'll have to convert the web site to web application first. See this link for explanation how to do it. It might take some effort, but I think the web application format is much easier to work with, so it's definately worth the effort.

我担心你必须先将网站转换为网络应用程序。请参阅此链接以获取解释如何执行此操作。这可能需要一些努力,但我认为Web应用程序格式更容易使用,所以它绝对值得付出努力。

#1


10  

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.

关于如何让MVC与ASP.NET Web应用程序一起使用,有很多博客文章。但是仍然存在使用普通ASP.NET网站项目而不是Web应用程序项目的情况。

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

以下是使用asp.net网站项目启用MVC 3的步骤

1. Install ASP.NET MVC 3

1.安装ASP.NET MVC 3

2. Modify web.config

2.修改web.config

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

在Visual Studio中打开web.config并在该部分中添加以下行

如何将asp.net mvc集成到Web站点项目中

3. Modify global.asax

3.修改global.asax

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

接下来,您需要在global.asax中添加MVC触发器的代码(如果它不存在则创建一个)

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

在<%@ Application Language =“C#”%>之后添加以下行

如何将asp.net mvc集成到Web站点项目中

Add the following after

之后添加以下内容

如何将asp.net mvc集成到Web站点项目中

add the following inside application_start

在application_start中添加以下内容

如何将asp.net mvc集成到Web站点项目中

At this point, your global.asax should look like

此时,您的global.asax应该是这样的

如何将asp.net mvc集成到Web站点项目中

4. Creating the controller

4.创建控制器

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

因为这是一个网站项目,编译是在运行时,所以你必须在App_Code文件夹中创建控制器而不是主站点中的普通Controller文件夹

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

请注意,您的控制器类需要以Controller关键字结束。在示例中,使用controller =“Home”,控制器的类名必须是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

要添加第一个控制器,请右键单击App_Code文件夹并创建一个文件名为HomeController.cs的新类。

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

将以下代码粘贴到HomeController.cs中(替换所有内容)

如何将asp.net mvc集成到Web站点项目中

5. Test the site

5.测试网站

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

现在您已生成路由并创建了控制器,请浏览到localhost / home。你应该看到“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

希望这对你有帮助

#2


5  

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.

将整个网站转换为Web应用程序(WAP)可能会很痛苦,因此我不建议这样做。您可以尝试@Grievoushead提出的使MVC在Web站点项目中工作的其他建议(可以工作),但我会建议另一个建议。

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.

而不是试图“合并”网站和MVC WAP,尝试保持它们大多是分开的,但共享相同的文件夹。您可以执行以下操作: - 首先在单独的文件夹中创建MVC项目 - 将其中的所有文件复制到您的WebSite中。您可能需要手动合并像web.config和global.asax这样的coupe文件。 - 然后你想要处理你的MVC代码,你在VS中打开csproj。但是,如果您想处理原始代码,只需将该文件夹作为网站打开即可。

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

有点不寻常,但根据具体情况,这可能是一个很好的方法。

#3


1  

I'm afraid that you'll have to convert the web site to web application first. See this link for explanation how to do it. It might take some effort, but I think the web application format is much easier to work with, so it's definately worth the effort.

我担心你必须先将网站转换为网络应用程序。请参阅此链接以获取解释如何执行此操作。这可能需要一些努力,但我认为Web应用程序格式更容易使用,所以它绝对值得付出努力。