[英]c# ASP.NET - API Controllers - should Route start with "/" or not?
I have a remedial question.我有一个补救问题。 In our code base, I'm seeing different API controllers and sometimes, devs are using routes that start with "/" and other times not.
在我们的代码库中,我看到了不同的 API 控制器,有时,开发人员使用以“/”开头的路由,而其他时候则不是。
From what I can tell, regardless of whether the endpoint starts with "/" or not, they all are discoverable by this same URI据我所知,无论端点是否以“/”开头,它们都可以通过同一个 URI 发现
https://localhost:123/nameofcontroller
Sample C# Code:示例 C# 代码:
[Route("/widgets/tools/calc")]
or [Route("widgets/tools/calc")]或 [Route("widgets/tools/calc")]
Does it matter?有关系吗?
EDIT 1编辑 1
So after some additional reading it seems that we are using attribute routing... cuz we define the routes in the controller cs file, like this: (please correct me if i'm wrong)所以经过一些额外的阅读之后,似乎我们正在使用属性路由......因为我们在控制器cs文件中定义了路由,如下所示:(如果我错了,请纠正我)
controller1.cs控制器1.cs
[HttpGet]
[Route("/widgets/{widgetID}/report
controller2.cs控制器2.cs
[HttpGet]
[Route("widgets/tools/calc
But I'm still trying to understand what the diff is between routes that start with "/" and those that don't.但我仍在尝试了解以“/”开头的路线与不以“/”开头的路线之间的差异。
Read the comments in the code below:阅读下面代码中的注释:
namespace API.Controllers
{
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
/// <summary>
/// I prefer 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 specifying the controller route it has no effect on the uri. ... whitout 指定控制器路由它对 uri 没有影响。 With specifying the controller route, the uris will look like this:
指定控制器路由后,uris 将如下所示:
GET: /widgets/{widgetID}/report
GET: /controller2/widgets/tools/calc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.