0

我定义了以下路线:

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 它也不起作用。第一条路线仅在删除全部内容时才有效。

4

2 回答 2

0

定义路由的顺序很重要。所以你需要定义catchallCreateafter 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()));
于 2012-10-15T22:31:37.133 回答
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);

由于其他控制器在其他区域共享相同的名称,另一个答案的路由创建方法对我不起作用。

是的,这将使史蒂夫建议的包罗万象的路线变得不必要。

于 2016-05-10T23:45:59.137 回答