Asp.net MVC4 与 Web Form 并存
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 的请求,并能在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();
}
}}
- 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 } // 参数默认值
);
}
项目结构
转载于:https://www.cnblogs.com/hbb0b0/p/5090301.html
Asp.net MVC4 与 Web Form 并存相关推荐
- 为 MVC 和 Web Form 正名的【第二份】“大字报”
阅读说明书 1.本文将要说到的MVC和Web Form如果没有特殊说明,则表示为ASP.NET 框架下的ASP.NET MVC CTP和 Web Forms 的最基本形态和特征,不考虑特殊应用. 2. ...
- ASP.NET MVC4中调用WEB API的四个方法
当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各种客户端去使用服务已经是十分普遍的做法.就.NET而言,目前提供了Remoting,WebService和WCF服务,这都能开发出功能十分强 ...
- 嵌入Windows User Control到ASP.NET web form
嵌入Windows User Control到ASP.NET web form 实现步骤: 新建Windows User Control项目,加入自己想做的事情(一些控件.方法.属性) 新建或打开We ...
- asp.net 的 web form 过时了吗?
web form 其实是一个超前的设计. 每个厂商都希望服务器端和客户端采用同样的语言编程,这是为了商业利益考虑,如果能实现,对程序员来说,也是一个福音. sun 在服务器端有 java,在客户端就做 ...
- ASP.NET Web Form学习
ASP.NET Web Form学习 0.aspx与html 它如何工作? 从根本上讲,ASP.NET 页面与 HTML 完全相同. HTML 页面的扩展名是 .htm 或 .html.假如浏览器从服 ...
- ASP.NET MVC4 部分视图
ASP.NET MVC4 部分视图 2014-10-24 16:48 by 易code, 2663 阅读, 1 评论, 收藏, 编辑 [部分视图] ASP.NET MVC 里的部分视图,相当于 Web ...
- ASP.NET MVC4 IN ACTION学习笔记-第二波
ASPNET MVC4 视图基础(Views fundamentals) --忽然发现我的时间不够了,但是我还是完成了 原著:ASP.NET MVC 4 IN ACTION 本人能力有限,尽量将书中的 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(12)-系统日志和异常的处理②...
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(12)-系统日志和异常的处理② 上一讲我们做了日志与异常的结果显示列表,这一节我们讲要把他应用系统中来. ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(31)-MVC使用RDL报表
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(31)-MVC使用RDL报表 这次我们来演示MVC3怎么显示RDL报表,坑爹的微软把MVC升级到5都木有良 ...
最新文章
- pycharm 常用快捷键整理
- mysql file-pos_mysql-5.7 调整mysql的复制方式由master_log_file+master_log_pos 到gtid 详解
- android分析windowManager、window、viewGroup之间关系
- 组件中props参数的应用
- B/S开发中浏览器的工具利器
- java ssh 那一层应该捕获异常_ssh经典异常!
- 500份IT数字化转型资料,限量领!堪称最全,领不到会发慌
- Windows CE 5.0待机界面定制之一 - Taskbar的位置
- 官方client php api接口日记
- 数学:给你长度1~n的线段 组成三角形最长边长度不超过你的n三角形有多少个
- 《Python黑帽子》python3代码实现(第五章)
- 计算机网络职业生涯规划1000字,计算机专业职业生涯规划1000字
- mysql查询范围数据_MySQL通过POIN数据类型查询指定范围内数据
- It was either not specified and/or could not be found for the javaType (java.util.List) : jdbcType
- 当我们给别人提意见时,要注意些什么?
- 从零开始学JSON(修订版)
- 2021 Geek re
- Ant Design Vue 动态路由
- nginx监听80端口转发到tomcat 8080端口
- EFM32 LEUART波特率计算