mvc3 routing priorty / dot参数问题

时间:2012-10-15 22:18:41

标签: asp.net-mvc routing

我定义了以下路线:

context.MapRoute("routeCreate", "{aval}/anArea/aController/Create/{val.Id}", new { action = "Create", controller = "aController" });

在定义以下全部捕获路径之前执行哪些功能:

context.MapRoute("catchallCreate", "{aval}/anArea/{controller}/Create/", new { action = "Create"});

路由由以下方式调用:

RedirectToAction("Create", new {val.Id});

结果URL转到?Id = 1而不是/ 1,它似乎没有拿起“val”。不再是。

我在想使用{val.Id}可能有问题,因为我无法使用该参数语法创建约束。

更新

也许我仍然遗漏了一些东西,定义了以下路线我仍然看到解决方案?Id = 1而不是/ 1

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.Routes.Add(
            new Route(
                "{aval}/anArea/aController/Create/{val.Id}",
                new RouteValueDictionary()
                    {
                        { "action", "Create" },
                        { "controller", "aController" },
                        { "val.Id", UrlParameter.Optional }
                    },
                null,
                new RouteValueDictionary() { { "area", "anArea" } },
                new MvcRouteHandler()));
// catchall
context.MapRoute("Create", "{aVal}/anArea/{controller}/Create", new { action = "Create" });

即使删除了Optional,它也不起作用。第一条路线仅在移除了捕获物时才有效。

2 个答案:

答案 0 :(得分:0)

定义路线的顺序is important。因此,您需要在catchallCreate之后定义routeCreate

您可能考虑的另一件事是将val.Id定义为可选:

routes.Add(new Route("{aval}/anArea/aController/Create/{val.Id}",
                     new RouteValueDictionary()
                         {
                             {"action", "Create"},
                             {"controller", "aController"},
                             {"val.Id", UrlParameter.Optional}
                         },
                     new MvcRouteHandler()));

答案 1 :(得分:0)

我最近遇到了类似的情况,这是我在所有搜索中找到的最接近的事情。

我的问题更多是因为尝试为我的点参数提供默认值而导致的问题。对我有用的是:

context.MapRoute("routeCreate", 
                 "{aval}/anArea/aController/Create/{val.Id}",
                 new 
                 {
                    action = "Create",
                    controller = "aController"
                 });

var route = (Route)context.Routes["routeCreate"];
route.Defaults.add("val.Id", UrlParameter.Optional);

other answer's路由创建方法对我不起作用,因为其他控制器在其他区域共享相同的名称。

是的,根据Steve的建议,这将使得全包路线变得不必要。