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

开发 后端
本文向您介绍如何将传统ASP.NET窗体应用迁移到ASP.NET MVC框架中;这样,可以省去从头创建ASP.NET MVC Web应用的时间。

不是所有的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 MVC在System.Web, System.Web.Routing, System.Web.Abstractions, 以及System.Web.Mvc四个名称空间下,这意味着,在现有ASP.NET应用程序中添加对这些库(assemblies)的引用,是组合应用这两种技术的起步。

ASP.NET MVC在ASP.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应用程序中添加Controllers,Views,Views/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=neutralPublicKeyToken=31BF3856AD364E35/> 
   </httpModules> 
 </system.web> 
</configuration> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.

请注意以下一行代码,它防止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();  
       }  
   }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
<%@ 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> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

 

【编辑推荐】

  1. ASP.NET MVC教程:创建TaskList应用程序
  2. ASP.NET MVC教程:理解模型、视图和控制器
  3. 在ASP.NET MVC中使用UpdatePanel
  4. ASP.Net MVC框架配置与分析
  5. 简单理解ASP.NET MVC基本知识
责任编辑:佚名 来源: geez的博客
相关推荐

2009-07-23 15:44:39

ASP.NET MVC

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2009-07-27 12:22:03

ASP.NET和ASPASP.NET入门教程

2009-07-20 12:59:53

ASP.NET MVCASP.NET框架的功

2009-07-29 16:08:07

ASP和ASP.NET

2009-07-22 17:45:35

ASP.NET教程

2009-07-27 13:39:06

Web窗体页ASP.NET

2009-07-22 13:24:24

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2009-07-28 17:17:19

ASP.NET概述

2009-08-03 14:22:33

什么是ASP.NET

2009-10-29 09:15:32

ASP.NET MVCDropDownLis

2009-07-20 15:30:11

ASP.NET应用

2009-07-22 16:34:36

使用T4ASP.NET MVC

2011-09-22 10:58:56

ASP.NET

2009-07-22 09:36:54

使用UpdataModASP.NET MVC

2009-07-22 13:16:04

MvcAjaxPaneASP.NET MVC
点赞
收藏

51CTO技术栈公众号