加入selfhosted Asp.NET MVC 5到现有的WinForms /控制台项目 [英] Adding selfhosted Asp.NET MVC 5 into an existing winforms/console project
问题描述
我如何将现有的WinForms项目中添加selfhosted Asp.Net MVC 5应用程序?
我已经有selfhosted MVC应用5作为一个单独的控制台应用程序正常工作。
How do I add a selfhosted Asp.Net MVC 5 application inside my existing winforms project? I already have the selfhosted mvc 5 app working fine as a seperate console app.
不过,现在我需要这个MVC 5项目加载到我现有的WinForms应用程序的子文件夹,我需要拨打电话,以我现有的业务逻辑类的控制器中。
But,now I need to load this MVC 5 project into a subfolder of my existing winforms application and I need make calls to my existing business logic classes inside the controller.
我想AP preciate任何指导。
I would appreciate any guidance.
感谢。
推荐答案
在约增加MVC支持添加到现有的WebForms应用程序的Web。这两个游戏很好地并排侧在同一个项目
On the web about adding MVC support to an existing WebForms application. The two play nicely side-by-side in the same project
首先,你需要让你的MVC项目的便携式区域。这个细节可以在这里找到 。一个便携式面积是包含的项目,通常是你的解决方案的一部分的DLL。便携式区域包含视图,控制器,模型,甚至JS脚本,CSS文件和图像。
First you would need to make your MVC project a portable area. Details about this can be found here. A Portable Area is a dll that contains items that would normally be part of your solution. Portable Areas contain Views, Controllers, Models, even JS Scripts, CSS files and images.
便携式面积缺点:
您让您的便携式区域内改变任何视图,JS文件,CSS文件,或图像每一次,你将需要重建。我强调这些组件,因为它们通常并不需要重建正在测试或开发的时候。
Every time you make a change to any View, JS File, CSS File, or image within your Portable Area, you will need to rebuild it. I emphasize these components because they do not normally need to be rebuilt when being tested or developed.
这会成为一个问题。如果你发现自己重新建立每次调整CSS时30 Second改变成为2分钟变化。使这些30,你已经捉襟见肘的工作价值15分钟到2小时。
This can become a problem. If you find yourself re-building every time you tweak CSS, 30 second changes become 2 minute changes. Make 30 of those and you've stretched 15 minutes worth of work into 2 hours.
接下来,你需要更新你的web.config文件。用以下替换默认的编译内容:
Next, you need to update your web.config file. Replace the default compilation section with the following:
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
还添加了一个页面元素和显示的命名空间:
Also add a pages element with the namespaces shown:
<pages>
<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
</namespaces>
</pages>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
和添加MVC HTTP处理程序和绑定重定向MVC:
and add the MVC HTTP handler and a binding redirect for MVC:
<httpHandlers>
<add verb="*" path="*.mvc"
validate="false" type="System.Web.Mvc.MvcHttpHandler,
System.Web.Mvc, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/>
</httpHandlers>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
现在我们Global.asax文件,我们需要注册便携领域:
Now in our Global.asax file we need to register the portable area:
public class SomeHybrid: System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
现在,在这一点上我可以访问这两个网页。该WebForms的页面是磁盘上的文件,所以ASP.NET路由时,我/default.aspx直接在这个页面传递的请求。在ASP.NET路由引擎从事这样我也能打到/首页/索引。
Now, at this point I can visit both pages. The WebForms page is a file on disk, so ASP.NET routing passes requests directly on to this page when I /default.aspx. The ASP.NET Routing engine is engaged so I can also hit /Home/Index.
如果我想获得幻想,你可以让你有pretty的URL访问我的WebForms页面时,以及添加PageRoute。只是在Global.asax添加路由这样。确保像这些简单的途径来第一次,作为默认的ASP.NET MVC路线很贪心,并会吞噬一个简单的URL如/计算器
If I want to get fancy, You can add a PageRoute so You have pretty URLs when visit my WebForms pages as well. Just add a route in the Global.asax like this. Make sure that simple routes like these come first, as the default ASP.NET MVC route is very "greedy" and would gobble up a simple URL like /calculator
routes.MapPageRoute("WebFormThing", "Calculator", "~/Default.aspx");
这篇关于加入selfhosted Asp.NET MVC 5到现有的WinForms /控制台项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!