asp.net-mvc 结合 ASP.Net MVC 和 WebForms
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2203411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combine ASP.Net MVC with WebForms
提问by Zyphrax
Is it possible to create a MVC root application (Portal with masterpages and themes) and add a couple of WebForms based subprojects (we already have an existing WebForms application that we would like to integrate into the Portal)?
是否可以创建一个 MVC 根应用程序(带有母版和主题的门户)并添加几个基于 WebForms 的子项目(我们已经有一个想要集成到门户中的现有 WebForms 应用程序)?
How would you centralize navigation (sitemaps, url routing)?
How would you share the masterpages?
How would you refer to resources (~ issues etc.)?
您将如何集中导航(站点地图、网址路由)?
你会如何分享母版?
您如何引用资源(~问题等)?
采纳答案by jeroenh
Combining web forms with MVC is entirely possible. See this blog post by Scott Hanselmanfor an introduction.
将 Web 表单与 MVC 相结合是完全可能的。有关介绍,请参阅Scott Hanselman 的这篇博文。
- Sharing master pages: see this StackOverflow question
- routing: In ASP.Net 4.0, routing has been enabled for web forms page routes (scottgu's blog)
- 共享母版页:请参阅此 StackOverflow 问题
- 路由:在 ASP.Net 4.0 中,Web 表单页面路由已启用路由(scottgu 的博客)
回答by Matt
For reference I managed to accomplish this task yesterday - add MVC functionality to an existing WebForms website, that is - so thought I would write in case it is of any use to anybody.
作为参考,我昨天设法完成了这项任务 - 将 MVC 功能添加到现有的 WebForms 网站,也就是说 - 所以我想我会写,以防它对任何人有用。
People say: "the best way is to rewrite your web forms application as MVC" but there is often no business case for this when the application is well established and has grew over time to be quite a monster (that still works well I might add). In my case, we had been asked to implement a new function into this application - and having been developing in MVC on other projects recently - I wanted to build this new function in MVC. This is what I did:
人们说:“最好的方法是将你的 Web 表单应用程序重写为 MVC”,但是当应用程序已经很好地建立并且随着时间的推移已经成长为一个怪物时,通常没有商业案例(这仍然有效,我可能会补充)。就我而言,我们被要求在这个应用程序中实现一个新功能 - 最近一直在其他项目的 MVC 中开发 - 我想在 MVC 中构建这个新功能。这就是我所做的:
Create a new ASP.NET MVC 4 Applicationand selected the Empty Template(with Razor)
Once loaded, right-clicked References> Browse and then navigated to the Binfolder of my existing website. I then added in the DLL's I was using in the existing site. For reference, I am using AjaxControlToolkit so decided to add in the latest one from NuGet. This should then auto-update the web.config file
I then opened up Windows Explorerand navigated to the root folder of my existing website. Using Visual Studio, I then replicated the top-level folder structureof the existing site (minus the Bin folder) resulting in the 2 sites having the same top-level folders (but the new project having the MVC App_Start, Controllers, Models and Views folders also, but not Bin obviously).
Once setup, I copied the sub-folders and files from the corresponding top-level folder in Windows Explorerand pasted them into the solution. If you do it the way, the project allows you to copy in sub-folders and content directly, adding existing item via visual studio only copies in files thus saving time. Please note: for the root folder, I did NOTcopy in web.configor Global.asax, but copied in all other files (like default.aspx, etc)
Once all content was copied, I opened up web.config in the new project and web.config in the existing site. I copied into the new web.config anything specific to the existing website (appSettings, connectionStrings, namespaces, membership info, etc). The same applied to the Global.asax files.
I attempted a build and fixed anything I'd missed.
Once everything was right (and I had shot down a few Yellow screens of death) I noticed that I got the page not found error when loading default.aspx. This is down to routing, here is the changes I made to the Routing info (located in Global.ascx MVC 3, or App_Start > RouteConfig.cs MVC 4):
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // This informs MVC Routing Engine to send any requests for .aspx page to the WebForms engine routes.IgnoreRoute("{resource}.aspx/{*pathInfo}"); routes.IgnoreRoute("{resource}.aspx"); // Important change here: removed 'controller = "Home"' from defaults. This is the key for loading // root default page. With default setting removed '/' is NOT matched, so default.aspx will load instead routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { action = "Index", id = UrlParameter.Optional } ); }
创建一个新的ASP.NET MVC 4 应用程序并选择空模板(使用 Razor)
加载后,右键单击References> Browse,然后导航到我现有网站的Bin文件夹。然后我添加了我在现有站点中使用的 DLL。作为参考,我正在使用 AjaxControlToolkit,因此决定添加来自 NuGet 的最新版本。这应该然后自动更新 web.config 文件
然后我打开Windows 资源管理器并导航到我现有网站的根文件夹。使用 Visual Studio,然后我复制了现有站点的顶级文件夹结构(减去 Bin 文件夹),导致 2 个站点具有相同的顶级文件夹(但新项目具有 MVC App_Start、Controllers、Models 和 Views文件夹也是如此,但显然不是 Bin)。
设置完成后,我从Windows 资源管理器中相应的顶级文件夹中复制了子文件夹和文件,并将它们粘贴到解决方案中。如果您这样做,该项目允许您直接复制子文件夹和内容,通过 Visual Studio 添加现有项目仅复制文件,从而节省时间。请注意:根文件夹,我不是在复制web.config文件或Global.asax中,但在所有其他文件复制(如Default.aspx的,等等)
复制所有内容后,我在新项目中打开 web.config,在现有站点中打开 web.config。我将任何特定于现有网站的内容(appSettings、connectionStrings、命名空间、会员信息等)复制到新的 web.config 中。这同样适用于 Global.asax 文件。
我尝试了一个构建并修复了我错过的任何东西。
一旦一切正常(并且我已经击落了一些黄色的死亡屏幕),我注意到加载default.aspx时出现页面未找到错误。这取决于路由,这里是我对路由信息所做的更改(位于 Global.ascx MVC 3,或 App_Start > RouteConfig.cs MVC 4):
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // This informs MVC Routing Engine to send any requests for .aspx page to the WebForms engine routes.IgnoreRoute("{resource}.aspx/{*pathInfo}"); routes.IgnoreRoute("{resource}.aspx"); // Important change here: removed 'controller = "Home"' from defaults. This is the key for loading // root default page. With default setting removed '/' is NOT matched, so default.aspx will load instead routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { action = "Index", id = UrlParameter.Optional } ); }
With all these steps taken, the site loaded and ran as it always did, but I now have a ASP.NET WebForms and MVC hybrid. I am sure there are probably easier ways of accomplishing this task, but I tried other methods and failed. This way took me around 20 minutes and I was up and running... well sat and coding :)
采取所有这些步骤后,该站点像往常一样加载和运行,但我现在有一个 ASP.NET WebForms 和 MVC 混合。我确信可能有更简单的方法来完成这项任务,但我尝试了其他方法并失败了。这种方式花了我大约 20 分钟,我开始运行......很好地坐着和编码:)
回答by Odd
Great answer above. The other thing you could consider is migrating from ASP.NET to ASP.NET MVC, in which case this post might help youmigrate the project files.
上面的答案很好。您可以考虑的另一件事是从 ASP.NET 迁移到 ASP.NET MVC,在这种情况下,这篇文章可能会帮助您迁移项目文件。
回答by Hitesh Nagpal
I was too stucked in this situation, after all i got a solution which worked, first of all copy web.configin your views folder.
我在这种情况下太卡住了,毕竟我得到了一个有效的解决方案,首先复制web.config到您的视图文件夹中。
After that open Route.config(Routeconfig)file, there you will find one method RegisterRoutes, keep it as it is and create another method named RegisterRoutes2(), and copy paste the below method code from the mvc application:
在打开Route.config(Routeconfig)文件之后,您将找到一个方法RegisterRoutes,保持原样并创建另一个名为 的方法RegisterRoutes2(),然后从 mvc 应用程序复制粘贴以下方法代码:
public static void RegisterRoutes2(RouteCollection routes)
{...}
in some cases method name in mvc app would be RegisterRoutes.
If above code does not work, copy paste it from your machine's mvc application.
Then, go to global.asaxfile, ther you will find RegisterRoutes()has been called, simple now , also call RegisterRoutes2().
And you are done, restart your application, or clean build, it will run.
在某些情况下,mvc 应用程序中的方法名称将是 RegisterRoutes。如果以上代码不起作用,请从您机器的 mvc 应用程序中复制粘贴。然后,去global.asax文件,你会发现RegisterRoutes()已经调用了,现在简单了,也调用了RegisterRoutes2()。完成后,重新启动应用程序或清理构建,它将运行。

