简体   繁体   English

将ASP.NET MVC区域添加到ASP.NET Web窗体现有项目

[英]Add ASP.NET MVC Area to ASP.NET Web Forms existing project

I have a website developed in ASP.NET Web Forms with .NET FrameWork 4.5.1 我有一个使用.NET FrameWork 4.5.1在ASP.NET Web窗体中开发的网站

We have a requirement to build a replica of the site for mobile devices (responsive doesn't work for us in this case) so I ended up thinking of doind this part of the site with ASP.NET MVC. 我们需要为移动设备构建站点的副本(在这种情况下,响应式不适用于我们),因此我最终想到了用ASP.NET MVC来覆盖站点的这一部分。

Note before you continue: I know there are a few questions answered about this topic and in the web there are some articles on how to mix them. 在继续之前,请注意: 我知道有关此主题的一些问题,并且在网络上有一些有关如何混合它们的文章。 But none of them works for me because I'm trying to use the One ASP.NET Framework to avoid going through a lot of manual work. 但是它们都不适合我,因为我试图使用One ASP.NET Framework来避免进行大量的手工工作。

So, what I did was: 因此,我所做的是:

  1. Add the Nuget Package for MVC in my ASP.NET WebForms project 在我的ASP.NET WebForms项目中为MVC添加Nuget程序包
  2. Once added, I configured an MVC Area to contain my Mobile site in MVC 添加后,我将MVC区域配置为将我的移动网站包含在MVC中

The project structure looks like this: 项目结构如下所示:

项目结构

And here comes my problem: If I run any of the ASPX pages, they run ok. 这是我的问题:如果我运行任何ASPX页面,它们都可以正常运行。 When I go to the /Mobile/Home/Index it says the resource is not found. 当我转到/ Mobile / Home / Index时,它说找不到资源。 I suspect this is because the App_Start folder doesn't exists in my project and Starup.cs isn't there either so the website doesn't know anything about MVC routing. 我怀疑这是因为App_Start文件夹在我的项目中不存在,并且Starup.cs也没有,所以网站对MVC路由一无所知。

Question

  1. How can I add the App_Start folder and the Starup.cs with defaults values without suffering the process (avoiding copy/paste or manual processes)? 如何添加具有默认值的App_Start文件夹和Starup.cs而不经历该过程(避免复制/粘贴或手动过程)?

  2. How can I make this both (web type projects) live together as they say it can be done with the One ASP.NET Framework?. 我如何使这两个(网络类型的项目)一起生活,就像他们说的那样可以使用One ASP.NET Framework完成?

Thanks! 谢谢!

You'll need to register the areas in your Global.asax (add it through the Add Item menu option if it does not exist): 您需要在Global.asax中注册区域(如果不存在,请通过“添加项目”菜单选项添加区域):

public class Global : HttpApplication
{

    protected void Application_Start(object sender, EventArgs e)
    {
        AreaRegistration.RegisterAllAreas();

    }

}

This will enable the link to your Mobile @ localhost/{ProjectName}/Mobile/Home 这将启用到您的Mobile @ localhost/{ProjectName}/Mobile/Home

If I was going to mix the two within the same project, I'd start with an MVC project , and have the ASP.Net pages be within their own subfolder. 如果要在同一个项目中混合使用两者,那么我将从MVC项目开始 ,并将ASP.Net页面放在其自己的子文件夹中。

But perhaps you should consider having 2 separate projects, one per "application". 但是也许您应该考虑拥有2个单独的项目,每个“应用程序”一个。

So now a separate application might be deployed to MainApplicationFolder\\SomeSubfolder. 因此,现在可以将一个单独的应用程序部署到MainApplicationFolder \\ SomeSubfolder。

Of course, you might also then want to consider something like the following for your links then: 当然,然后,您可能还需要考虑以下类似的链接:

@VirtualPathUtility.ToAbsolute("~/SomeController/SomeAction")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM