【问题标题】:Routing for REST style urlsREST 样式 url 的路由
【发布时间】:2017-03-24 17:21:32
【问题描述】:

我希望设置路由以允许以下操作:

/entity/id          //id is Guid
/entity/id          //id is int32
/entity/actionname  //actionname is string matching neither Guid nor Int

简单,我想,这就是 RouteConstraints 解决的问题:

routes.MapRoute(
    name: "entity/id:guid",
    template: "{controller}/{id:guid}",
    defaults:new{action="Index"});
routes.MapRoute(
    name: "entity/id:int",
    template: "{controller}/{id:int}",
    defaults: new { action = "Index" });
routes.MapRoute(
    name: "entity/action",
    template: "{controller=home}/{action=index}");

我认为顺序很重要:更具体的匹配必须在实体/动作路由之前,否则会匹配所有内容。

但这不起作用。

@Url.Action("", "entity", new { id = Guid.NewGuid() })

结果

entity?id=00000000-0000-0000-0000-000000000000

而不是

entity/00000000-0000-0000-0000-000000000000

我该如何解决?

(我使用的是 asp.net Core 1.1,尽管我相信这个问题在 MVC4 中是有效的)

【问题讨论】:

标签: asp.net-mvc asp.net-core-mvc asp.net-mvc-routing


【解决方案1】:

如果您传递动作名称"Index" 或传递null 作为第一个参数,它将产生预期的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2020-01-27
    • 1970-01-01
    • 2011-07-02
    • 2013-02-18
    • 2011-06-28
    • 2011-11-18
    相关资源
    最近更新 更多