将ASP.NET MVC 3集成到现有的升级的ASP.NET 4 Web Forms应用程序中

本文介绍了如何在已升级到ASP.NET 4的Web Forms应用程序中添加ASP.NET MVC 3页面,无需重写整个应用。通过使用升级向导,比较默认的MVC应用和升级后的Web Forms应用的配置,然后添加必要的引用、web.config配置和全局注册,可以实现Web Forms和MVC的混合使用。
摘要由CSDN通过智能技术生成

I got an interesting question recently from a gentleman who has an existing ASP.NET WebForms 2.0 application that works just fine. He's upgraded it to ASP.NET 4 and it still works great, but now he wants to add some ASP.NET MVC pages to it. He doesn't want to rewrite the application.

我最近从一位先生那里得到了一个有趣的问题,他的先生拥有一个可以正常运行的ASP.NET WebForms 2.0应用程序。 他已经将其升级到ASP.NET 4,并且仍然可以正常运行,但是现在他想向其中添加一些ASP.NET MVC页面。 他不想重写应用程序。

A few years ago I did a post on "Hybrid" ASP.NET applications. The goal was to reinforce the point that you can have ASP.NET applications that are both WebForms and MVC (as well as WCF and ASMX Web Services and on and on.) While the File|New Project dialog gives you a choice between This and That, in fact it's all ASP.NET underneath. You are welcome to mix and match "cafeteria style" and create apps in any combination you'd like.

几年前,我写了一篇有关“混合” ASP.NET应用程序的文章。 目的是要强调一点,即可以同时具有WebForms和MVC(以及WCF和ASMX Web Services等)的ASP.NET应用程序。而File | New Project对话框使您可以在This和那实际上就是所有ASP.NET。 欢迎您混合并匹配“自助餐厅样式”,并以所需的任何组合创建应用程序。

The easiest way to add ASP.NET MVC 3 to an upgraded ASP.NET 2.0 WebForms application:

将ASP.NET MVC 3添加到升级的ASP.NET 2.0 WebForms应用程序中的最简单方法是:

  • Run the Upgrade Wizard (open the  Visual Studio2008 Web Application in Visual Studio 2010)

    运行升级向导(在Visual Studio 2010中打开Visual Studio2008 Web应用程序)
  • Create a default ASP.NET MVC application for reference (you'll throw it away later)

    创建一个默认的ASP.NET MVC应用程序以供参考(稍后将其丢弃)
  • Use a differencing tool like Beyond Compare to integrate the new web.config entries from the ASP.NET MVC sections into the upgraded ASP.NET WebForms application

    使用Beyond Compare之类的差异化工具将来自ASP.NET MVC部分的新web.config条目集成到升级的ASP.NET WebForms应用程序中

  • Dance

    舞蹈

Here's the longer more detailed version of the above.

这是上面的更详细的版本。

升级ASP.NET 2.0 WebForms应用程序 (Upgrading an ASP.NET 2.0 WebForms Application)

I've created a simple Visual Studio 2008 ASP.NET WebForms Application running under .NET 2. It's a simple calculator.

我已经创建了一个在.NET 2下运行的简单Visual Studio 2008 ASP.NET WebForms应用程序。它是一个简单的计算器。

It works nicely. Now, open this application in Visual Studio 2010. You'll get the Conversion/Upgrade Wizard.

效果很好。 现在,在Visual Studio 2010中打开此应用程序。您将获得“转换/升级向导”。

Next, Next, Yada, Yada, Yada, Finish. You'll get an prompt to upgrade the 2.0 application to .NET Framework 4. Click Yes.

接下来,下一步,Yada,Yada,Yada,完成。 您将得到提示,将2.0应用程序升级到.NET Framework4。单击“是”。

Here's the same WebForms application, now upgraded in Visual Studio 2010. It still runs and it's still WebForms. Or, more accurately, it continues to  be ASP.NET.

这是相同的WebForms应用程序,现在已在Visual Studio 2010中进行了升级。它仍在运行,并且仍然是WebForms。 或者,更准确地说,它仍然是ASP.NET。

 

I'm going to take a new default ASP.NET MVC 3 application and Beyond Compare and compare the upgraded app with the default app.

我将使用新的默认ASP.NET MVC 3应用程序和“超越比较”,并将升级后的应用程序与默认应用程序进行比较。

I'll copy over these folders and files:

我将复制以下文件夹和文件:

  • Content

    内容
  • Controllers

    控制器
  • Models

    楷模
  • Scripts

    剧本
  • Views

    观看次数
  • Global.asax, Global.asax.cs

    Global.asax,Global.asax.cs

Now, here's the before and after references from the upgraded application. The old on the left and the new on the right.

现在,这里是升级后的应用程序中的参考文献。 左边是旧的,右边是新的。

New References
References

Here's the references I added.

这是我添加的参考。

  • Microsoft.CSharp

    微软CSharp

    • (as this was a C# app)

      (因为这是一个C#应用)
  • System.Web.Mvc

    System.Web.Mvc

    • From \Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies

      从\ Program Files(x86)\ Microsoft ASP.NET \ ASP.NET MVC 3 \ Assemblies
  • System.Web.WebPages and System.Web.Razor

    System.Web.WebPages和System.Web.Razor

    • From \Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies

      从\ Program Files(x86)\ Microsoft ASP.NET \ ASP.NET网页\ v1.0 \ Assemblies
  • System.ComponentModel.DataAnnotations

    System.ComponentModel.DataAnnotations

Next, add these sections to the Web.config. Again, it's easier to use a diff tool and you might have a little trial and error.

接下来,将这些部分添加到Web.config。 同样,使用差异工具会更容易,并且您可能会遇到一些尝试和错误。

Thought: This might be a nice NuGet package for someone to make...

认为:对于某人来说可能是一个不错的NuGet软件包...

Add these settings in appSettings:

在appSettings中添加以下设置:

<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

Add these assembly elements under compilation:

在编译中添加以下程序集元素:

<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.Helpers, Version=1.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=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>

Add these namespaces in pages:

在页面中添加以下名称空间:

<system.web>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
</namespaces>
</pages>
</system.web>

If you're running IIS7 at some point, which I'm sure you will, add these:

如果您确定要运行IIS7,请确保添加以下内容:

<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

And finally add this assembly binding redirect, just in case you've got ASP.NET MVC 1 or 2 assemblies in your Global Assembly Cache (GAC).

最后,添加此程序集绑定重定向,以防万一在全局程序集缓存(GAC)中有ASP.NET MVC 1或2程序集。

<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Also, make sure you merge in the Global.asax.cs so that your Routes are registered at application startup.

另外,请确保您在Global.asax.cs中进行合并,以便在应用程序启动时注册您的路由。

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);
}
}

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.

现在,我可以同时访问这两个页面。 WebForms页面是磁盘上的文件,因此当我/default.aspx时,ASP.NET路由将请求直接传递到此页面上。 启用了ASP.NET路由引擎,因此我也可以点击/ Home / Index。

If I want to get fancy, I can add a PageRoute so I 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

如果我想花哨的话,可以添加PageRoute,这样在访问WebForms页面时也可以使用漂亮的URL。 只需像这样在Global.asax中添加一条路由。 确保首先出现这样的简单路由,因为默认的ASP.NET MVC路由非常“贪婪”,并且会吞噬一个简单的URL,例如/ calculator

routes.MapPageRoute("WebFormThing", "Calculator", "~/Default.aspx");

Now I can visit /Calculator and the request is routed to /Default.aspx. And of course, my ASP.NET MVC 3 Razor pages like /Home/Index work also.

现在,我可以访问/ Calculator并将请求路由到/Default.aspx。 当然,我的ASP.NET MVC 3 Razor页面(如/ Home / Index)也可以工作。

Finally, just to make the point, here's the Default.aspx from the WebForms part of my new app next to the source for a Razor page.

最后,为了说明这一点,这是我的新应用程序的WebForms部分中的Default.aspx,位于Razor页面的源代码旁边。

You CAN have it all, Dear Reader. Enjoy.

亲爱的读者,您可以拥有所有这些。 请享用。

翻译自: https://www.hanselman.com/blog/integrating-aspnet-mvc-3-into-existing-upgraded-aspnet-4-web-forms-applications

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值