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

推荐订阅源

博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
S
SegmentFault 最新的问题
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
D
Docker
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
宝玉的分享
宝玉的分享
腾讯CDC
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss
V
V2EX
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Y
Y Combinator Blog
P
Proofpoint News Feed
T
Tor Project blog
AWS News Blog
AWS News Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
T
Threat Research - Cisco Blogs
B
Blog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 司徒正美
H
Help Net Security
C
Cisco Blogs
C
Check Point Blog
S
Secure Thoughts

博客园 - yo

Silverlight 缺陷 - yo - 博客园 SilverLight Controls WF 4.0 中 Persistence 异常 - yo SilverLight框架初探-View - yo - 博客园 SilverLight框架初探-RiaService SilverLight框架初探-ViewModel SilverLight框架初探 与客户“调情” AG_E_PARSER_BAD_PROPERTY_VALUE 数据契约的序列化 WCF客户端配置问题 关闭EXCEL进程 母版页中控件ID获取 - yo - 博客园 Reportviewer - Error: ASP.NET session has expired - yo SQL CLR C# DLL动态调用 Sharepoint List faults - yo 出错页面webpar的t删除 quickpart
定义属于自己的Routing
yo · 2009-12-24 · via 博客园 - yo

 在.Net3.5SP1中,新增加了一个DLL:System.Web.Routing,只要我们在项目中添加该引用,那么就可以实现如MVC项目中的路由选择。

 public class Global : System.Web.HttpApplication {

  protected void Application_Start(object sender, EventArgs e)
        {
   RegisterRoutes(RouteTable.Routes);
  }

        /// <summary>
        /// 添加路由规则
        /// </summary>
        /// <param name="routes">路由集合</param>
  public static void RegisterRoutes(RouteCollection routes)
        {
   routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
            routes.Add("webforms", new Route("{controller}/{action}.aspx", new RouteValueDictionary(new { controller = "Account", action = "ViewPage1" }), new RouteHandler()));
  }

        /// <summary>
        /// 实现路由规则
        /// 这里有俩种方式:第一种方式直接通过System.Web.Compilation.BuildManager来创建返回一个IHttpHandler;
        /// 第二种方式将请求上下文交给一个继承自IHttpHandler的类来处理
        /// </summary>
        public class RouteHandler : IRouteHandler
        {

            #region IRouteHandler Members

            public IHttpHandler GetHttpHandler(RequestContext requestContext)
            {
                return new PageHandler(requestContext);
                //Route route = (Route)requestContext.RouteData.Route;
                //string path = "/" + route.Defaults["Controller"] + "/" + route.Defaults["action"] + ".aspx";
                //IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(path, typeof(System.Web.UI.Page)) as IHttpHandler;
                //return hander;
            }

            #endregion
        }

        public class PageHandler : IHttpHandler
        {
            public RequestContext requestContext { get; set; }
            public PageHandler(RequestContext context)
            {
                this.requestContext = context;
            }

            #region IHttpHandler Members

            public bool IsReusable
            {
                get { return false; }
            }

            public void ProcessRequest(HttpContext context)
            {
                Route route = (Route)requestContext.RouteData.Route;
                RouteValueDictionary routeVale = requestContext.RouteData.Values;
                string path = string.Format("/Views/{0}/{1}.aspx",
                                                     routeVale.ContainsKey("controller")
                                                        ? route.Defaults["controller"]
                                                        : routeVale["controller"],
                                                     routeVale.ContainsKey("action")
                                                        ? route.Defaults["action"]
                                                        : routeVale["action"]);
                context.Server.Execute(path);
            }

            #endregion
        }
 }

posted on 2009-12-24 16:03  yo  阅读(247)  评论()    收藏  举报