当前位置: 首页 > news >正文

ASP.NET MVC - 旧形式URL的路由

原文地址:ASP.NET MVC - Legacy Url Routing
原文作者:matthaw

本文地址:ASP.NET MVC - 旧形式URL的路由
本文作者:Q.Lee.lulu

最近我们已经将大部分我们ASP.NET Web Form页面转换为使用ASP.NET MVC。当这个并不是一个小的工程的时候,使用一个新的URL结构并支持旧的URL是很有需要的。这个想法是,当你点击一个不存在的页面的时候,你会被定位到MVC里面合适的Controller和Action中去。

流程

image

1. 一个来自你的网站的旧形式的URL请求.例如:http://www.server.com/Users/Login.aspx
2. ASP.NET routing 拦截这个请求并匹配你RouteCollection中的一个Route  
3. 并不是使用 MvcRouteHandler, 而是 LegacyRouteHandler 被调用.
4. 使用 LegacyRouteHandler, 它将会使用你指定的Route重定向名来产生MVC URL,并发出一个定位到http://www.server.com/site/login的HTTP 301 请求.

Routing

首先,我们需要定义我们的旧版Route类。这是必需的,因为我们需要暴露一个额外的属性来允许我们的routing处理器来找到正确的MVC route。

// 旧形式URL的route类,暴露一个RedirectActionName
public class LegacyRoute : Route 
{
	public LegacyRoute(string url, string redirectActionName, IRouteHandler routeHandler)
		: base(url, routeHandler)
	{
		RedirectActionName = redirectActionName;
	}
	public string RedirectActionName { get; set; }
}

然后,我们需要定义route handler和关联的http handler。Route handler继承自IRouteHandler,而当创建你的旧形式URL routing的时候要使用这个类。Http handler继承自MvcHandler,因为它会为我们提供一些关键的信息,例如RequestContext。你也许会注意到你需要从request上copy所有的querystring参数(代码中并没有写出来)。这是必需的一步,因为GetVirtualPath方法调用时会需要所有的route data(来自RouteData.Values)并尝试利用它来创建URL。

// The legacy route handler, used for getting the HttpHandler for the request   
public class LegacyRouteHandler : IRouteHandler
{
	public IHttpHandler GetHttpHandler(RequestContext requestContext)
	{
		return new LegacyHandler(requestContext)
	}
}
// The legacy HttpHandler that handles the request public class LegacyHandler : MvcHandler { public LegacyHandler(RequestContext requestContext) : base(requestContext) { } protected override void ProcessRequest(HttpContextBase httpContext) { string redirectActionName = ((LegacyRoute)RequestContext.RouteData.Route).RedirectActionName; // ... copy all of the querystring parameters and put them within RouteContext.RouteData.Values VirtualPathData data = RouteTable.Routes.GetVirtualPath(RouteContext, redirectActionName, RouteContext.RouteData.Values); httpContext.Status = "301 Moved Permanently"; httpContext.AppendHeader("Location", data.VirtualPath); } }

最后,你需要在Global.asax文件中创建你的routes。记住,在配置routing的时候这是必需的一步。

public void RegisterRoutes(RouteCollection routes) 
{
	routes.MapRoute("Login", "site/login", new {
		controller = "Users",
		action = "DisplayLogin"
	});
	routes.Add("", new LegacyRoute(
		"Users/Login.aspx",
		"Login",
		new LegacyRouteHandler()
	));
}

 

就是这样。当一个请求来到的时候,你会在Fiddler中看到如下的信息:

  1. A request on "Users/Login.aspx"
  2. A HTTP 301, with a header "Location" and value of "site/login"
  3. A request on "site/login"

Final Thoughts

Granted, there's more you can do with this - like creating your own extension methods like MapRoute and doing better handling of finding the route, but this should get you started. Also, I'm writing the code off the top of my head, so there's no guarantee that any of it works as-is. Please let me know if you have any other thoughts.

Lastly, for those wondering why are we using a HTTP 301 status code? Well read up on them. "301 Moved Permanently" indicates "that all future requests should be directed to the given URI." While your end users will not see any difference other than a new URL in the browser, the 301 status code more aimed towards search engines to update their URL's in their indexes.

转载于:https://www.cnblogs.com/QLeelulu/archive/2008/05/24/1206467.html

相关文章:

  • v4l2 spec 中文 Ch01【转】
  • 在WinForm中使用Web Services 来实现 软件 自动升级(C#)
  • CISCO路由器license激活图解教程
  • [LeetCode] Wiggle Sort
  • ASP得到当前页面完整地址
  • Photoshop脚本 关闭所有已打开的文档
  • oop_day06_抽象类、接口_20150814
  • 电商直播ios app耗电发热比较评测
  • C# 中的委托和事件
  • 【extjs6学习笔记】1.6 初始:本地化
  • 我所知道的SharePoint feature(1)
  • 新增题目功能模块总结
  • 手机的各项性能测试
  • MyEclipse 历史版本
  • INSERT 失败,因为下列 SET 选项的设置不正确: 'ARITHABORT'
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • 【mysql】环境安装、服务启动、密码设置
  • 〔开发系列〕一次关于小程序开发的深度总结
  • echarts的各种常用效果展示
  • emacs初体验
  • ES学习笔记(12)--Symbol
  • JavaScript设计模式与开发实践系列之策略模式
  • react 代码优化(一) ——事件处理
  • SpiderData 2019年2月13日 DApp数据排行榜
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • Webpack4 学习笔记 - 01:webpack的安装和简单配置
  • windows-nginx-https-本地配置
  • 从0到1:PostCSS 插件开发最佳实践
  • 从零搭建Koa2 Server
  • 分类模型——Logistics Regression
  • 分享几个不错的工具
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 前端之Sass/Scss实战笔记
  • 微信支付JSAPI,实测!终极方案
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • C# - 为值类型重定义相等性
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • ​软考-高级-信息系统项目管理师教程 第四版【第14章-项目沟通管理-思维导图】​
  • # 计算机视觉入门
  • #if 1...#endif
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (done) 两个矩阵 “相似” 是什么意思?
  • (二)c52学习之旅-简单了解单片机
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (附源码)基于SSM多源异构数据关联技术构建智能校园-计算机毕设 64366
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (一)Neo4j下载安装以及初次使用
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .NET Reactor简单使用教程
  • .net 前台table如何加一列下拉框_如何用Word编辑参考文献