【问题标题】:How can I do a multiple route constraints in ASP.NET MVC Core?如何在 ASP.NET MVC Core 中进行多路由约束?
【发布时间】:2017-10-22 07:17:59
【问题描述】:

我在互联网上进行了很多搜索,以找出像The Customer Id should be a guid and required 这样的约束的方法,以便最终得到[HttpGet("{customerId:guid|required}")],但不幸的是没有任何解决方案可以这样做。来自 Microsoft 的文档https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routingRoute Constraint Reference 部分也没有说明这一点。任何人都可以像我刚才描述的那样做多个约束值吗?提前谢谢你。

【问题讨论】:

  • 我认为可以通过在路由条件{customerId:required} 中使用required 来实现,并且在行动中您可以指定传入customerId 的类型,如Get([FromRoute]Guid customerId),稍后您还可以查看ModelState.IsValid
  • 这方面的文档在哪里?

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


【解决方案1】:

只需用冒号分隔约束...{customerId:guid:required}。带参数的约束用这种方式也很好用……{id:int:range(100, 999)}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 2019-02-10
    • 2011-06-01
    • 2020-11-02
    相关资源
    最近更新 更多