c# ASP.NET - API 控制器 - 路由是否应该以“/”开头?
c# ASP.NET - API Controllers - should Route start with "/" or not?
我有一个补救问题。
在我们的代码库中,我看到了不同的 API 控制器,有时,开发人员使用以“/”开头的路由,有时则不是。
据我所知,无论端点是否以“/”开头,它们都可以通过相同的 URI 发现
https://localhost:123/nameofcontroller
示例 C# 代码:
[Route("/widgets/tools/calc")]
或
[路线("widgets/tools/calc")]
重要吗?
编辑 1
所以经过一些额外的阅读,我们似乎正在使用属性路由...因为我们在控制器 cs 文件中定义路由,如下所示:(如果我错了请纠正我)
controller1.cs
[HttpGet]
[Route("/widgets/{widgetID}/report
controller2.cs
[HttpGet]
[Route("widgets/tools/calc
但我仍在尝试了解以“/”开头的路由与不以“/”开头的路由之间的区别。
阅读下面代码中的注释:
namespace API.Controllers
{
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
/// <summary>
/// I preffer to use route attributes on controllers ...
/// ===========================================================================================
/// By default the mvc pattern looks so: {controller}/{action} + parameters if defined,
/// ===========================================================================================
/// </summary>
[ApiController, Route("/widgets")]
public class WidgetsController : ControllerBase
{
/// <summary>
/// ... and for specifying additional parameters using of http methods attributes ...
/// ===========================================================================================
/// when you use template without leading backslash it is appended to the controller route
/// and you have GET: /widgets/all instead of just GET: /widgets
/// ===========================================================================================
/// [HttpGet]
/// [Route("all")]
/// </summary>
[HttpGet("all")]
public ActionResult<IEnumerable<object>> Get()
{
return this.Ok(new [] { "w1", "w2", "etc." });
}
/// <summary>
/// ... but at the end both of them are valid ...
/// ===========================================================================================
/// when you use template with leading backslash the controller route is now OVERWRITTEN
/// and now looks so: GET: /all/criteria
/// ===========================================================================================
/// [HttpGet]
/// [Route("/all")]
/// </summary>
[HttpGet("/all/{filter}")]
public ActionResult<IEnumerable<object>> Get(string filter)
{
return this.Ok(new[] { "w1", "w2" });
}
/// <summary>
/// ===========================================================================================
/// it is helpfull for defining route parameters like bellow
/// here the route will looks like GET /widgets/123
/// so you can have multiple get methods with different parameters
/// ===========================================================================================
/// </summary>
[HttpGet("{widgetId}")]
public ActionResult<object> Get(int widgetId)
{
return this.Ok(new { widgetId });
}
}
}
... whitout 指定控制器路由它对 uri 没有影响。通过指定控制器路由,uris 将如下所示:
GET: /widgets/{widgetID}/report
GET: /controller2/widgets/tools/calc
我有一个补救问题。 在我们的代码库中,我看到了不同的 API 控制器,有时,开发人员使用以“/”开头的路由,有时则不是。
据我所知,无论端点是否以“/”开头,它们都可以通过相同的 URI 发现
https://localhost:123/nameofcontroller
示例 C# 代码:
[Route("/widgets/tools/calc")]
或 [路线("widgets/tools/calc")]
重要吗?
编辑 1
所以经过一些额外的阅读,我们似乎正在使用属性路由...因为我们在控制器 cs 文件中定义路由,如下所示:(如果我错了请纠正我)
controller1.cs
[HttpGet]
[Route("/widgets/{widgetID}/report
controller2.cs
[HttpGet]
[Route("widgets/tools/calc
但我仍在尝试了解以“/”开头的路由与不以“/”开头的路由之间的区别。
阅读下面代码中的注释:
namespace API.Controllers
{
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
/// <summary>
/// I preffer to use route attributes on controllers ...
/// ===========================================================================================
/// By default the mvc pattern looks so: {controller}/{action} + parameters if defined,
/// ===========================================================================================
/// </summary>
[ApiController, Route("/widgets")]
public class WidgetsController : ControllerBase
{
/// <summary>
/// ... and for specifying additional parameters using of http methods attributes ...
/// ===========================================================================================
/// when you use template without leading backslash it is appended to the controller route
/// and you have GET: /widgets/all instead of just GET: /widgets
/// ===========================================================================================
/// [HttpGet]
/// [Route("all")]
/// </summary>
[HttpGet("all")]
public ActionResult<IEnumerable<object>> Get()
{
return this.Ok(new [] { "w1", "w2", "etc." });
}
/// <summary>
/// ... but at the end both of them are valid ...
/// ===========================================================================================
/// when you use template with leading backslash the controller route is now OVERWRITTEN
/// and now looks so: GET: /all/criteria
/// ===========================================================================================
/// [HttpGet]
/// [Route("/all")]
/// </summary>
[HttpGet("/all/{filter}")]
public ActionResult<IEnumerable<object>> Get(string filter)
{
return this.Ok(new[] { "w1", "w2" });
}
/// <summary>
/// ===========================================================================================
/// it is helpfull for defining route parameters like bellow
/// here the route will looks like GET /widgets/123
/// so you can have multiple get methods with different parameters
/// ===========================================================================================
/// </summary>
[HttpGet("{widgetId}")]
public ActionResult<object> Get(int widgetId)
{
return this.Ok(new { widgetId });
}
}
}
... whitout 指定控制器路由它对 uri 没有影响。通过指定控制器路由,uris 将如下所示:
GET: /widgets/{widgetID}/report
GET: /controller2/widgets/tools/calc