在同一应用中混合使用ASP.NET窗体和ASP.NET MVC 转自:geez的个人空间
2011-02-26 15:47
483 查看
在同一应用中混合使用ASP.NET窗体和ASP.NET MVC
不是所有的ASP.NET MVC Web[/u]应用程序都需要从头创建,也许您希望将现有的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=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);
}
}
}
请注意以下一行代码[/u],它防止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[/u]">
<title></title>
</head>
<body>
<div>
<h1><%=Html.Encode(ViewData["Message"]) %></h1>
</div>
</body>
</html>
不是所有的ASP.NET MVC Web[/u]应用程序都需要从头创建,也许您希望将现有的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=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);
}
}
}
请注意以下一行代码[/u],它防止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[/u]">
<title></title>
</head>
<body>
<div>
<h1><%=Html.Encode(ViewData["Message"]) %></h1>
</div>
</body>
</html>
相关文章推荐
- ASP.NET窗体和ASP.NET MVC在同一应用中混合使用
- ASP.NET窗体和ASP.NET MVC在同一应用中混合使用
- 使用ASP.NET MVC开发混合移动应用
- docker4dotnet #3 在macOS上使用Visual Studio Code和Docker开发asp.net core和mysql应用
- 使用 IntraWeb (1) - 先测试如何部署为 Asp.Net 的应用
- 日常使用的asp.net应用
- 使用反射将业务对象绑定到 ASP.NET 窗体控件
- 20150812 Asp.net 父窗体获取子窗体的返回值,更新父窗体文本控件(应用)
- 使用asp.net 2.0和SQL SERVER 2005构建多层应用
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
- ASP.Net Ajax应用初步:使用AJAX直接调用后台方法
- 使用Installshield制作asp,asp.net应用的安装程序
- 在 ASP.NET 中使用 SQL Server 2000 Analysis Services 和 Office XP 构造 OLAP 报表应用
- docker4dotnet #3 在macOS上使用Visual Studio Code和Docker开发asp.net core和mysql应用
- 在ASP.NET1.0/2.0里使用DIV层元素弹出窗体
- 在 ASP.NET MVC 应用中使用 NInject 注入 ASMX 类型的 Web Service
- ASP.NET页面进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)第1/2页
- asp.net中使用窗体身份验证
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试
- 转:ASP.NET 2.0:使用用户控件和定制的Web部件个人化你的门户网站