高级路由ASP教程和示例 [英] Advanced ASP Routing tutorials and examples

查看:256
本文介绍了高级路由ASP教程和示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个主要的障碍我似乎有最近越来越我的头周围的一些在为一些基于MVC应用我一直在开发更复杂的布线要求。
我在寻找合适的套教程走我通过它得到一个完整的认识问题。

One of major hurdles I seem to be having recently is getting my head around some of the more complex routing requirements for some MVC based applications I've been developing. I'm having problems finding the right set of tutorials to walk me through it to get a complete understanding.

我想找到是一套完整的教程,一切从基本的(控制器/动作/ ID)路由到先进的。

What I'd like to find is a complete set of tutorials for everything routing from basic (controller/action/id) to advanced.

什么我打电话高级路由的一个例子是喜欢的东西:

An example of what I'm calling advanced routing is things like:

/博客/年/月/日/标题 - 将映射到控制器:博客和行动:和参数:标题

/blog/year/month/day/title - would map to controller: blog and action: post and as parameters: year, month, day and title

/博客/标题 - 将映射到控制器:博客和行动:和参数:标题

/blog/title - would map to controller: blog and action: post and as parameters: title

/标题 - 将映射到控制器:博客和行动:发表和参数:标题

/title - would map to controller: blog and action: post and as parameters: title

我可以映射每个可能的设置为使用一个数据库在全球的显式路由,但好像它打败具有路​​由引擎路由到正确的地方的点。我宁愿一次定义规则。

I could map each possible set to an explicit route in global using a database, but that seems like it's defeating the point of having the routing engine route to the correct place. I'd rather define the rule once.

推荐答案

我不明白,为什么在需要的时候你不只是定义他们每个人作为一个独立的路线,使用常规的前pression。例如在 /博客/年/月/日/标题和区分 /博客/标题

I don't understand, why can't you just define each one of them as a separate route, using regular expression when needed. For example to differentiate between the /blog/year/month/day/title and /blog/title.

这些集合每个人都是一个独立的情况下,你需要告诉MVC如何处理每一个。您可以通过做一次定义规则的Global.asax.cs 文件:

Each one of those sets is a separate case, and you'll need to tell MVC what to do with each one. You can do this by defining the rule once in the Global.asax.cs file:

对于第一种情况: /博客/年/月/日/标题

routes.MapRoute(
    "Blog Full Route", // Route name
    "blog/{year}/{month}/{day}/{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    new {year = @"\d+", month= @"\d+", day = @"\d+"} // Constrain parameters with RegEx patterns
    );

有关第二种情况: /博客/标题

routes.MapRoute(
    "Blog Title Route", // Route name
    "blog/{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );

有关最后一种情况: /标题

routes.MapRoute(
    "Title Route", // Route name
    "{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );

诀窍是在底部把论文路线严格按照此顺序,用最少的具体。改变顺序会导致错误的路径被使用(特别是在最后两个)。如果最后的情况与第二种情况下切换,该类型的URL 博客/ SomeTitle 将路线行动与博客作为标题。

The trick is putting theses routes in this exact order, with the least specific at the bottom. Changing the order would result in the wrong route being used (specifically in the last two). If the last case was switched with the second case, URLS of the type blog/SomeTitle would route to the post action with blog as the title.

每当你创造的东西路线,请记住以下几点:

Whenever you're creating a route for something, keep the following in mind:


  1. 约束路由参数与正则表达式

  2. 非常意识到路线顺序(哪条路线来之前它)

  3. 的花括号 {东西} 表示动作参数

  1. Constraint route parameters with RegEx
  2. Be very aware of route order (which route comes before which)
  3. The squiggly brackets {something} denote action parameters

有一些很好的教程:

这篇关于高级路由ASP教程和示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆