代码改变世界

使用指定的 URL 模式和处理程序类初始化 Route 类的新实例(仅供参考,高手绕过)

2009-12-21 10:35  jinze  阅读(258)  评论(0编辑  收藏  举报

创建及使用Routing类,注意,该类只能在全局处理程序的 Application_Start(object sender, EventArgs e) 方法中使用才是有意义的:

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

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add(
new Route
    (
         
"Category/{action}/{categoryName}"
         , 
new CategoryRouteHandler()
    ));
}

 

需要注意的是,Route 的构造函数 (String, IRouteHandler)中,IRouteHandler是一个接口,不能直接实例化,但可以创建新的类来实现它的 GetHttpHandler 方法

 

 public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            
return new MyPage(requestContext);
        }

 

RequestContext是客户请求的上下文,直接由URL来提供。(基于.Net 3.5 SP1)

用电子邮件联系Andy