在同一应用中混合使用ASP.NET窗体和ASP.NET MVC 转自:geez的个人空间

在同一应用中混合使用ASP.NET窗体和ASP.NET MVC

不是所有的ASP.NET MVC Web应用程序都需要从头创建,也许您希望将现有的ASP.NET应用移植到ASP.NET MVC。在同一个应用同时使用ASP.NE窗体和ASP.NET MVC可能吗?答案是-完全可以。

在同一个应用同时使用ASP.NE窗体和ASP.NET MVC不仅是可行的,而且是相当容易的,因为ASP.NET MVC是在ASP.NET基础上构建的框架。实际上它们仅有一个关键处不同:ASP.NET是在System.Web名称空间下,而ASP.NET MVCSystem.Web, System.Web.Routing, System.Web.Abstractions, 以及System.Web.Mvc四个名称空间下,这意味着,在现有ASP.NET应用程序中添加对这些库(assemblies)的引用,是组合应用这两种技术的起步。

ASP.NET MVCASP.NET基础上构建的另外一个优点是:应用数据能够很容易地在两种技术间共享。例如,Session这个状态对象在两种技术下都可以使用,这样使得通过Session状态来高效地共享数据。

一个ASP.NET窗体应用程序可以通过执行以下步骤,成为一个ASP.NET MVC应用程序。

1.   在现有的ASP.NET应用程序中,添加以下库的引用:System.Web.Routing.dll, System.Web.Abstractions.dll, andSystem.Web.Mvc.dll

2.   在现有ASP.NET应用程序中添加ControllersViewsViews/Shared文件夹;

3.   修改web.config文件,如下(注意是修改ASP.NET应用程序现有的web.config,而不是覆盖掉):

< ?xml version="1.0"?>
<configuration>
 <system.web>
   <compilation debug="false">
     <assemblies>
       <add assembly="System.Core, Version=3.5.0.0, Culture=neutral,
                      PublicKeyToken=B77A5C561934E089"/>
       <add assembly="System.Web.Extensions,
                      Version=3.5.0.0, Culture=neutral,
                      PublicKeyToken=31BF3856AD364E35"/>
       <add assembly="System.Web.Abstractions,
                      Version=3.5.0.0, Culture=neutral,
                      PublicKeyToken=31BF3856AD364E35"/>
       <add assembly="System.Web.Routing,
                      Version=3.5.0.0, Culture=neutral,
                      PublicKeyToken=31BF3856AD364E35"/>
     </assemblies>
   </compilation>
   <pages>
     <namespaces>
       <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.Linq"/>
       <add namespace="System.Collections.Generic"/>
     </namespaces>
   </pages>
   <httpModules>
     <add name="UrlRoutingModule"
          type="System.Web.Routing.UrlRoutingModule,
                System.Web.Routing, Version=3.5.0.0,
                Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
   </httpModules>
 </system.web>
</configuration>

4.   配置路由。往Global.asax中加入默认的ASP.NET MVC全局应用程序类(Global Application Class)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MixingBothWorldsExample
{
   public class Global : System.Web.HttpApplication
   {
       public static void RegisterRoutes(RouteCollection routes)
       {
           routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
           routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
           routes.MapRoute(
              "Default",
// Route name
              "{controller}/{action}/{id}",
// URL with parameters
              new { controller = "Home", action = "Index", id = "" }
// Parameter defaults
               );
       }
       protected void Application_Start()
       {
           RegisterRoutes(RouteTable.Routes);
       }
   }
}

请注意以下一行代码,它防止ASP.NET窗体的请求被路由到ASP.NET MVC中:

routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");

以上四个步骤完成后,我们便可以开始添加ASP.NET MVC控制器和视图了,例如:

using System.Web.Mvc;
namespace MixingBothWorldsExample.Controllers
{
   public class HomeController : Controller
   {
       public ActionResult Index()
       {
           ViewData["Message"] = "This is ASP.NET MVC!";
           return View();
       }
   }
}

 

<%@ Page Language="C#"
   AutoEventWireup="true"
   CodeBehind="Index.aspx.cs"
   Inherits="MixingBothWorldsExample.Views.Home.Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
   <title></title>
</head>
<body>
   <div>
       <h1><%=Html.Encode(ViewData["Message"]) %></h1>
   </div>
</body>
</html>


原文地址:https://www.cnblogs.com/cwy173/p/1965778.html