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

推荐订阅源

Y
Y Combinator Blog
IT之家
IT之家
博客园_首页
量子位
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
博客园 - 聂微东
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 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)  评论()    收藏  举报