我如何使用约束在ASP.net MVC 4 RouteConfig.cs? [英] How do I use constraints in ASP.net MVC 4 RouteConfig.cs?

查看:93
本文介绍了我如何使用约束在ASP.net MVC 4 RouteConfig.cs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用最新的asp.net的MVC 4架构工作的一些布线约束。在App_Start有一个名为RouteConfig.cs文件。

如果我从下面我的​​例子中删除的约束部分,该URL的作品。但我需要添加一些限制,使得一切的网址犯规匹配。

应该工作:/视频/等级/ 1

Shold不起作用:/视频/ 2458 /文本GOES-这里

这是我有:

  // URL:/视频/等级/ 1
routes.MapRoute(
    名称:视频,
    网址:视频/ {排序} / {页},
    默认:新{控制器=VideoList,行动=索引,排序= UrlParameter.Optional,页= UrlParameter.Optional},
    限制:新{排序= @[A-ZA-Z],页= @\\ D +}
);


解决方案

如果你想在同一航线上的多个可选参数,你会遇到麻烦,因为你的URL必须始终指定要使用第二个的第一个。仅仅因为你使用约束不会从评估参数阻止它,而是它不匹配这条路线。

拿这个例如: /视频/ 3

在此尝试匹配,找到视频,并且说,好吧,我还是匹配。然后,它着眼于下一个参数,这是排序,并得到值3,然后检查其针对约束。约束失败,所以它说:有机磷农药,我不匹配这条路线,并将其移动到下一个路径。为了不定义排序参数指定的页面,您应该定义2路。

  // URL:/视频/等级/ 1
routes.MapRoute(
    名称:视频,
    网址:视频/ {排序} / {页},
    默认:新{控制器=VideoList,行动=索引,页= UrlParameter.Optional},
    限制:新{排序= @[A-ZA-Z] +,页= @\\ D +}
);// URL:/视频/ 1
routes.MapRoute(
    名称:视频,
    网址:视频/ {页},
    默认:新{控制器=VideoList,行动=索引,排序=实际的默认排序值,页= UrlParameter.Optional},
    限制:新{页= @\\ D +}
);

我把最具体航线第一可能时,用最少的具体结束,但是在这种情况下,因为限制的顺序不应该的问题。我的意思具体是最定义的值,所以在这种情况下,你的必须的定义中的第一个路由的排序,你也可以指定页面,所以它比只用路线更具体页面的参数。

I'm trying to get some routing constraints working using the latest asp.net mvc 4 architecture. Under App_Start there is a file called RouteConfig.cs.

If I remove the constraints section from my example below, the url works. But I need to add some constraints so that the url doesnt match on everything.

Should work: /videos/rating/1

Shold NOT work: /videos/2458/Text-Goes-Here

This is what I have:

//URL: /videos/rating/1
routes.MapRoute(
    name: "Videos",
    url: "videos/{Sort}/{Page}",
    defaults: new { controller = "VideoList", action = "Index", Sort = UrlParameter.Optional, Page = UrlParameter.Optional },
    constraints: new { Sort = @"[a-zA-Z]", Page = @"\d+"}
);

解决方案

If you want multiple optional parameters on the same route, you will run into trouble because your urls must always specify the first one in order to use the second one. Just because you use constraints doesn't stop it from evaluating the parameters, it instead fails to match this route.

Take this for example: /videos/3

When this is trying to match, it finds videos, and says, "OK, I still match". Then it looks at the next parameter, which is Sort and it gets the value 3, then checks it against the constraint. The constraint fails, and so it says "OPPS, I don't match this route", and it moves on to the next route. In order to specify the page without the sort parameter defined, you should instead define 2 routes.

//URL: /videos/rating/1
routes.MapRoute(
    name: "Videos",
    url: "videos/{Sort}/{Page}",
    defaults: new { controller = "VideoList", action = "Index", Page = UrlParameter.Optional },
    constraints: new { Sort = @"[a-zA-Z]+", Page = @"\d+"}
);

//URL: /videos/1
routes.MapRoute(
    name: "Videos",
    url: "videos/{Page}",
    defaults: new { controller = "VideoList", action = "Index", Sort = "the actual default sort value", Page = UrlParameter.Optional },
    constraints: new { Page = @"\d+"}
);

I put the most specific routes first when possible and end with the least specific, but in this case the order should not matter because of the constraints. What I mean by specific is most defined values, so in this case you must define the sort in the first route, and you also can specify the page, so it is more specific than the route with just the page parameter.

这篇关于我如何使用约束在ASP.net MVC 4 RouteConfig.cs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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