没有使用正确的映射命中ASP.NET Route

时间:2014-12-13 06:09:28

标签: .net asp.net-mvc routing html-helper t4mvc

为什么:

@Url.Action("Divisions", "Widgets", new {version = 1, eventId = Model.Event.Id, slug = Model.Event.Slug})

生成这个:

http://localhost:2227/widgets/divisions?version=1&eventid=36295&slug=notifications

当我的路线是这样的。

  routes.MapRoute(
                "DefaultWidget",
                "widgets/v{version}/{action}",
                new { controller = "Widgets", action = "NotFound", version = 1, slug = "event"},
                new { version = @"\d+" }
                );

并且WidgetsController中的动作是这样的

 public virtual ActionResult Divisions(int version, int? eventId, string slug)
        {
            return GetDivisions(eventId, new WidgetEventViewModel(version));
        }

路线应如下所示:

http://localhost:2227/widgets/v1/divisions?eventid=36295

1 个答案:

答案 0 :(得分:0)

从路径定义中删除slug = "event"url中没有占位符,因此路径不匹配)

或者将url参数更改为

"widgets/v{version}/{action}/{eventid}/{slug}",

将生成(假设版本= 1,偶数= 36295和slug =" MySlug")

/widgets/v1/divisions/36295/MySlug