Web Forms 與 MVC 的asp.net 基礎架構是相同的。MVC 的路由機制並不只MVC 特有的,它與WebForm 也是共享相同的路由機制。Web Forms 的Http請求針對的是物理文件,每個頁面都實現IhttpHandler,MVC 的Http 請求是針對Controller的Action方法,最終依靠MvcHandler 實現對請求的響應。由於Web Forms 與MVC 的基礎架構相同,所以Web Forms 與 MVC 可以並存在同一個站點下。
現實情況中存在很多這種需求。歷史維護的工程中的老頁面使用Web Forms ,重寫所有的頁面為MVC 方式,成本過大,老工程有添加了新的模塊,新的模塊完全可以使用MVC 方式來寫。
如何演示MVC 與 Web Forms
並存在同一個站點下?
實現目標
- 能響應Web Forms 的請求,並能在Page 頁面使用 高大上的GridView
- 能響應MVC 請求,並能在View 頁面使用 Razor引擎
實現過程
1. 創建空的Asp.net Web Application
2. 添加 MVC 與Razor 相關的 dll
3. 配置Web.config
![]()
- Form
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyWebForm.aspx.cs" Inherits="MvcWithWebForm.WebForm.MyWebForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvTest" runat="server" >
</asp:GridView>
</div>
</form>
</body>
</html>
- Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace MvcWithWebForm.WebForm
{
public partial class MyWebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.BindData();
}
private void BindData()
{
this.gvTest.DataSource = this.GetCustomerList();
this.gvTest.DataBind();
}
public List<Customer> GetCustomerList()
{
List<Customer> list = new List<Customer>();
for (int i = 0; i < 10; i++)
{
Customer c = new Customer() { No = 1000 * i, Name = string.Format("b0b0-{0}",i.ToString()) };
list.Add(c);
}
return list;
}
}
public class Customer
{
public int No
{
get;
set;
}
public string Name
{
get;
set;
}
}
}
5. MVC
- Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcWithWebForm.Controllers
{
public class CustomerController:Controller
{
public ActionResult Index()
{
return View();
}
}
}
Controller 代碼的位置:必須放到App_Code目錄下
- View
@inherits System.Web.Mvc.WebViewPage
@{
ViewBag.Title = "Index";
}
<h2>MVC Index</h2>
<div>
@for (int i = 0; i < 10; i++)
{
@Html.Raw(string.Format("<div style=\"font-size:{0}pt\"> Hello,Mvc Razor</div>", (5*i).ToString()));
}
</div>
View的位置,必須放到 ~/Views/[Controller]/[ViewName]6. Global 配置 路由規則
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Mvc;
using RouteDebug;
namespace MvcWithWebForm
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
//全局路由表 忽略掉MVC 對asp.net Web Forms 請求
RouteTable.Routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
//MVC 路由規則
RouteTable.Routes.MapRoute(
"Customer",
"{controller}/{action}/{id}",
new { controller = "Customer", action = "Index", id = UrlParameter.Optional } // 參數默認值
);
}
項目結構
測試
如果已引用dll,在運行時找不到dll 請設置dll的復制屬性為復制到本地。

1 WebForm
2 MVC