1/1页1 跳转到查看:1417
发新话题 回复该主题
键盘左右键可以进行前后翻页操作
帮助

ASP.NET MVC URL Routing 学习

ASP.NET MVC URL Routing 学习


文/QLeelulu  出处/博客园

定义URL Routing

在一个route中,通过在大括号中放一个占位符来定义( { and } )。当解析URL的时候,符号"/"和"."被作为一个定义符来解析,而定义符之间的值则匹配到占位符中。route定义中不在大括号中的信息则作为常量值。
下面是一些示例URL:


 附件: 该内容需注册用户才可浏览,感谢您的支持!

通常,我们在Global.asax文件中的Application_Start事件中添加routes,这确保routes在程序启动的时候就可用,而且也允许在你进行单元测试的时候直接调用该方法。如果你想在单元测试的时候直接调用它,注册该routes的方法必需是静态的同时有一个RouteCollection参数。
下面的示例是Global.asax中的代码,演示了添加一个包含两个URL参数action 和 categoryName的Route对象:



引用:
protected 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参数的默认值

当你定义个一route的时候,你可以分配一个默认值给route的参数。默认值是当URL中没有包含参数的值的时候使用的。你可以在Route类中通过dictionary来设置Default属性来设置route的默认值:



引用:
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()
  )
    {
      Defaults = new RouteValueDictionary
          {{"categoryName", "food"}, {"action", "show"}}
    }
  );
}


当URL Routing处理URL Request的时候,上面route定义产生的结果如下表所示: 



 附件: 该内容需注册用户才可浏览,感谢您的支持!

您可能对 [Asp.Net] 版块的最新发帖也感兴趣:
• ASP.NET MVC 入门系列教程 • 简单实现网站首页图片切换新闻的效果
• ASP.NET 3.5 Enterprise Application Development with VS 2008 • ASP.NET 3.5 Application Architecture and Design
• 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie • ASP.NET MVC Step by Step中文版下载
• Asp.Net 中的报销多级审批工作流 (状态机版本) • Asp.Net 中的报销多级审批工作流
• ASP.NET 3.5 Unleashed(揭秘)PDF下载 • ASP.NET MVC in Action PDF
• ASP.NET MVC扩展之IControllerFactory和IActionInvoker • Asp.Net MVC 流程初探
• ASP.NET开发人员需要学习ASP.NET MVC么? • Asp.NET MVC and Asp.NET WebForms Features
• 深入MVC模式概念 • ASP.NET MVC中你必须知道的13个扩展点
• ASP.NET程序中常用的三十三种代码 • 前几天写了一个记事控件
• ASP.NET 进度条(附源代码) • 如何在 asp.net 中使用 WMI 功能,创建web站点等

TOP

 

回复:ASP.NET MVC URL Routing 学习

处理不确定个数的参数
有时候你需要处理一个URL包含的参数是不确定的URL请求。在你定义route的时候,你可以设置最后一个参数包含一个星号,使最后一个参数匹配URL中剩下的参数。例如:


 附件: 该内容需注册用户才可浏览,感谢您的支持!



在匹配URL的时候添加约束
通过添加约束使URL参数在我们的程序中能更好的工作。废话不多说了,直接看代码:



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

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add(new Route
    (
      "{locale}/{year}"
        , new ReportRouteHandler()
    )
      {
          Constraints = new RouteValueDictionary
          {{"locale", "{a-z}{2}-{A-Z}{2}"},{year, @"\d{4}"}}
      });
}


当URL Routing处理URL Request的时候,上面route定义产生的结果如下表所示:


 附件: 该内容需注册用户才可浏览,感谢您的支持!


从Routes中创建URL
当你想集中控制逻辑来构造URL时,你可以使用routes来产生URLs。通过传递一个dictionary的参数值给RouteCollection对象的GetVirtualPath方法来创建一个URL。GetVirtualPath方法查找RouteCollection对象中第一个route中跟dictionary匹配的参数,匹配的route被用来产生URL。还是看下面的示例:



引用:
public static void RegisterRoutes(RouteCollection routes)
{
  routes.Add(new Route
  (
    "Category/{action}/{categoryName}"
          new CategoryRouteHandler()
  )
    {
      Defaults = new RouteValueDictionary {{"categoryName", "food"},
          {"action", "show"}}
    }
  );
}


下面的示例演示了基于上面的route创建的URL:



引用:
HyperLink1.NavigateUrl = RouteTable.Routes.GetVirtualPath
  (context,
  new RouteValueDictionary {
    { "categoryName", "beverages" },
    {"action", "summarize" }}
  ).VirtualPath;


当代码运行的时候,HyperLink1控件将会包含值"Category/summarize/beverages"在NavigateUrl属性中。

TOP

 
1/1页1 跳转到
发表新主题 回复该主题