惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Cybersecurity and Infrastructure Security Agency CISA
N
News and Events Feed by Topic
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
Kaspersky official blog
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
A
Arctic Wolf
Security Latest
Security Latest
T
Threatpost
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
G
GRAHAM CLULEY
The Hacker News
The Hacker News
C
CERT Recently Published Vulnerability Notes
Know Your Adversary
Know Your Adversary
I
Intezer
Scott Helme
Scott Helme
T
Tenable Blog
NISL@THU
NISL@THU
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
N
News and Events Feed by Topic
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
Project Zero
Project Zero
Latest news
Latest news
Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Forbes - Security
Forbes - Security
Security Archives - TechRepublic
Security Archives - TechRepublic
AI
AI
S
Security Affairs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
D
Docker
P
Proofpoint News Feed
博客园 - Franky

博客园 - 俩醒叁醉

ASP.NET MVC 4 WebAPI. Support Areas in HttpControllerSelector SQL2000安装问题(转) sql server 2008安装需要一直重启。但重启后又没有达到效果。 为数据库中所有的用户数据表生成分页存储过程 SQL 2005 字段备注获取 __doPostBack方法解析 如何在三个月掌握三年的经验(转载&&笔记) jQuery Ajax的使用 JQuery资源网站 Cookie跨域、虚拟目录 深入分析跨域cookie的问题 CnBlogsDotText使用实例 轻松搭建博客平台-开源ASP.NET 博客Subtext 的安装 表达式目录树(源MSDN) Web 2.0 编程思想:16条法则 Control Adapter 以下代码提供查询数据库中是否存在某个值 MVC Controllers和Forms验证 CSharp——Lambda 表达式
URL Routing
俩醒叁醉 · 2009-03-15 · via 博客园 - 俩醒叁醉

URL Routing模型是用来将浏览器输入的请求(requests)映射为MVC controller actions。

1、使用默认的Route表

当你创建ASP.NET MVC应用程序时,就已经配置为使用URL Routing的应用程序。URL Routing创建于两个地方。

一、在Web.config启用URL Routing。在Web.config文件中存在4个和Routing关联的sections,system.web.httpModules 、system.web.httpHandlers 、system.webserver.modules、 和system.webserver.handlers。

二、Route表创建于Global.asax文件中

 public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*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);}
    }
对于这里参数如果不为空时,可以对应Action:Index(int id)和Index()。但是如果Action为:Index(int id)则传入的参数不能为空。
2、自定义URL Routing
创建一个匹配任何以/Archive/开头的Request的Route:
  public static void RegisterRoutes(RouteCollection routes)
  {
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     routes.MapRoute(
          "blog",                       //自定义route必须出现在default之前,否则它不会被调用
          "Archive/{entryDate}",        //匹配任何以'/Archive/’开头的请求
          new { controller = "Archive", action = "Entry" });
     routes.MapRoute(
          "Default",                                              // Route name
          "{controller}/{action}/{id}",                           // URL with parameters
          new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
     );
  }
自定义route必须出现在默认Route之前。
3、MVC的URL Routing实现

ControllerBase类实现了IController接口,而该接口只提供一个方法void Execute(RequestContext)。RequestContext基的构造方法需要的参数类型HttpContextBase和HttpContext很相似。

 public class RequestContext
    {
        public RequestContext(HttpContextBase httpContext, RouteData routeData);
        public HttpContextBase HttpContext { get; }
        public RouteData RouteData { get; }
    }

从这可以看出MVC中URL Routing介入的起始点就在RequestContext类。

    public class RouteData
    {
        public RouteData();
        public RouteData(RouteBase route, IRouteHandler routeHandler);
        public RouteValueDictionary DataTokens { get; }
        public RouteBase Route { get; set; }
        public IRouteHandler RouteHandler { get; set; }
        public RouteValueDictionary Values { get; }
        public string GetRequiredString(string valueName);
    }

RouteBase 类:

    public abstract class RouteBase
    {
        protected RouteBase();
        public abstract RouteData GetRouteData(HttpContextBase httpContext);
        public abstract VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values);
    }

要研究透URL Routing,首先就必须清晰上面RouteData类中提到RouteValueDictionary、IRouteHandler。Route初始化时使用RouteValueDictionary来加入默认值及规则到Route中,而IRouteHandler的则提供了一个GetHttpHandler方法来获取用于处理URL的IHttpHandler。其接口定义如下:

  public interface IRouteHandler
    {
        IHttpHandler GetHttpHandler(RequestContext requestContext);
    }

GetHttpHandler输入参数是RequestContext ,而IHttpHandler 的ProcessRequest方法处理的参数则是HttpContext。这也就是URL Routing首次和HttpContext打交道的地方。如是实现这个转换呢?

//在构造方法中传入RequestContext 参数,用以设置页面RequestContext

public class MyPage:IHttpHandler 
{ 
    public RequestContext RequestContext { get; private set; } 
    public MyPage(RequestContext context) 
    { 
            this.RequestContext = context; 
    } 
    #region IHttpHandler  
    public virtual void ProcessRequest(HttpContext context)
	{ //将访问页面如a_b_c_index.aspx转换为a/b/c/index.aspx
		context.Server.Execute(RequestContext.RouteData.Values["page"].ToString().Replace("_","/")+".aspx"); 
	} 
    public bool IsReusable { get { return false; } } 
    #endregion 
}
//实现IRouteHandler 
public class MyRouteHandler : IRouteHandler 
{ 
    #region IRouteHandler  
    public IHttpHandler GetHttpHandler(RequestContext requestContext) 
    { 
        return new MyPage(requestContext); 
    } 
    #endregion 
}
这样我们就可以在Golbal.asax的Application_Start事件中写如下代码
RouteTable.Routes.Add(new Route("{page}.aspx",new MyRouteHandler()));