ASP.NET WebForms 4.0支持单向路由功能(MVC)

简介: The ASP.NET Routing engine was added in .NET Framework 3.5 SP1. Now Microsoft have added better support for using the engine in ASP.

The ASP.NET Routing engine was added in .NET Framework 3.5 SP1. Now Microsoft have added better support for using the engine in ASP.NET WebForms 4.0 by using expression builders enabling bi-directional routing.

Channel9 have just published a new episode of 10-4 showing how to use this new functionality in ASP.NET 4.0. In the video Jonathan Carter walk through a simple example of how to enable this in an existing ASP.NET application.

Below is a summary of the code showed in the video. They use the typical example of how Product.aspx?category=jerseys can map to Product/Jerseys. Using the ASP.NET Routing engine this mapping can be added using the RouteTable in Application_Start:

 

RouteTable.Routes.Add("Product",
new Route("Product/{name}",
new PageRouteHandler("~/Product.aspx")));

To add bi-directional routing support today, users would have to use URL rewriting on the query string. However, in ASP.NET 4.0 users can register expression builders:

 

<system.web>
<compilation>
<expressionBuilders ...>
<add expressionPrefix="RouteUrl"
type="System.Web.Compilation.RouteUrlExpressionBuilder" />

<add expressionPrefix="RouteValue"
type="System.Web.Compilation.RouteValueExpressionBuilder" />
</expressionBuilders>
</compilation>
</system.web>

The first expression is for getting a URL and the second for getting a value. The $ sign is used to access expressions from an aspx page:

<asp:HyperLink NavigationUrl="<%$ RouteUrl:RouteName=Product, name=Jerseys" 
Text="Jerseys"
runat="server" />

To get the value from the name attribute, users use the Route object instead of the Request object:

RouteData.Values["name"];

or using an expression builder:

<%$ RouteValue:name %>

By using ASP.NET Routing and the new bi-directional support users can decouple URLs from a physical Web Form, allowing friendlier URLs and have search engines discover and use these.

 

版权

作者:灵动生活 郝宪玮

出处:http://www.cnblogs.com/ywqu

如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,

img_2c313bac282354945ea179a807d7e70d.jpg

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

相关文章
|
安全 机器人 数据安全/隐私保护
手机屏幕点击器,屏幕自动点击器,免费连点器(自动点击)【autojs】
完整UI界面:包含坐标录制、执行控制、参数设置等区域 坐标录制功能:实时捕捉屏幕点击位置并记录坐标
|
搜索推荐 关系型数据库 MySQL
MySQL 模糊查询新纪元:超越 LIKE+% 的高效探索
在数据库的日常操作中,模糊查询是一项不可或缺的功能,它允许我们根据不完全匹配的关键字来检索数据。传统上,MySQL 使用 LIKE 关键字配合 % 通配符来实现这一功能,虽然灵活但性能上往往不尽如人意,尤其是在处理大型数据集时。今天,我们将一起探索几种超越 LIKE+% 的模糊查询技术,以提升查询效率与用户体验。
994 2
|
关系型数据库 MySQL 分布式数据库
DataX下载安装
DataX下载安装
2402 0
|
监控 数据可视化 BI
Kubernetes Ingress 日志分析与监控的最佳实践
Ingress主要提供HTTP层(7层)路由功能,是目前K8s中HTTP/HTTPS服务的主流暴露方式。为简化广大用户对于Ingress日志分析与监控的门槛,阿里云容器服务和日志服务将Ingress日志打通,只需要应用一个yaml资源即可完成日志采集、分析、可视化等一整套Ingress日志方案的部署。
25098 0
Kubernetes Ingress 日志分析与监控的最佳实践
人工智能 缓存 前端开发
11944 62
人工智能 JavaScript 开发工具
4782 17
Web App开发 人工智能 API
1349 1
人工智能 Java BI
1425 1
开发工具 Swift git
1954 6

热门文章

最新文章