c# - 在 ASP.NET Core(以前的 ASP.NET 5)中嵌套路由的便捷方式是什么?

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

我们想要嵌套路由,因为我们有数据的层次结构。 然而,最直观的做法是用一个主 Controller 来控制一切。我们当然不希望那样。我们想要很好地包含作为单元的 Controller (遵循 OO 原则)。

例如,我们有这样一个 API 调用:

http://domain.com/api/customers/3510/status/

我们想要一个漂亮的状态 Controller ,它将有一组 Action 来传递与状态相关的信息。

我想出了一个方法来做到这一点:

[Route("api/customers/{aId:int}/status")]
public class StatusController
{
   [HttpGet("current")]
   public string GetCurrentStatus(int aId)
   {
     return "Status customer " + aId + " is foo bar". 
   }
}

但这是正确的方法吗?或者还有其他更方便的方法吗?

最佳答案

路线 [Route("api/customers/{aId:int}/status")] 在我看来没问题。

MVC 6 中的惯例似乎是返回一个 IActionResult:

   [HttpGet("current")]
   public IActionResult GetCurrentStatus(int aId)
   {
     return Content("Status customer " + aId + " is foo bar"); 
   }

关于c# - 在 ASP.NET Core(以前的 ASP.NET 5)中嵌套路由的便捷方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34943118/

相关文章:

c# - BeginXXX 中 FromAsync 的参数太多?

c# - 如何将 ComboBox 绑定(bind)到一列列表

c# - Rebus 中未处理的随机消息

c# - .NET MVC 自定义路由

javascript - 设置 Backbone 路由器

c# - 如何强制 float 和 int 具有小数点或尾随零

c# - 如何在 MVC5 中使用自定义属性路由重定向到路由

c# - 如何为特定路由 Asp.Net MVC 构造我的操作方法

spring - 想要在 JHipster 中创建 URL

c# - 为什么 Azure 在向应用程序发送请求之前要解码请求的 URL