MVC 4:自定义路线 [英] MVC 4: Custom Routes

查看:167
本文介绍了MVC 4:自定义路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了很多与MVC路线的问题,我在得到一个路由匹配的网址类似的问题。

I see a lot of problems with MVC routes and I'm having a similar problem getting a route to match a URL.

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

//default route
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute("Beer", "Beer/{beerid}", new { controller = "Beer", action = "Id", beerid = 0});

routes.MapRoute("Beer", "Beer/{beername}", new { controller = "Beer", action = "Name" });

BeerController方法

BeerController Methods

public ActionResult Id(int beerid)
public ActionResult Name(string beername)

如果我改变方法以下,

public ActionResult Id(int? id)    
public ActionResult Name(string id)

默认的路由适用于以下网址:

the default routing works with the following URLs:

http://localhost/Beer/Id/100
http://localhost/Beer/Name/Coors

但我要为仅仅是

http://localhost/Beer/100
http://localhost/Beer/Coors

任何想法?

推荐答案

因此​​,这里有两件事情。

So a couple things here.


  1. 具体路由应该放在之前更普遍的路线,因为匹配将被用来和航线在它们被添加的顺序检查的第一条路线。

  1. More specific routes should be placed before more general routes, because the first route that is matched will be used and routes are inspected in the order they are added.

如果您打算在您的网址没有提供动作的名称,那么你将需要做一些事情,以确保正确的路线是这样的目标将使用正确的默认值。你的情况,你可以使用一个路由约束两者之间的区别。试着改变你的啤酒ID路线是:

If you plan on not providing the name of the action in your URL then you will need to do something to ensure the correct route is targeted so the correct default value will be used. In your case you could use a route constraint to distinguish between the two. Try changing your beer id route to this:

routes.MapRoute(
    name: "Beer",
    url: "Beer/{beerid}",
    defaults: new { controller = "Beer", action = "Id", beerid = 0},
    constraints: new { beerid = @"\d+" }
);

的约束将确保该路径唯一匹配,其中所述第二段是由一个或多个数字两段网址。这条路线以及你的啤酒名称路线应放在默认路由之前。

The constraint will ensure that the route only matches two-segment URLs where the second segment is composed of one or more digits. This route as well as your route for beer name should be placed before the default route.

更新

我的配置似乎得到你想要的结果。的全部我的的RegisterRoutes 方法如下:

My configuration seems to be yielding the results you want. The entirety of my RegisterRoutes method is as follows:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    name: "Id",
    url: "Beer/{beerid}",
    defaults: new { controller = "Beer", action = "Id", beerid = 0 },
    constraints: new { beerid = @"\d+" }
);

routes.MapRoute(
    name: "Name",
    url: "Beer/{beername}",
    defaults: new { controller = "Beer", action = "Name" }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

这篇关于MVC 4:自定义路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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