繁体   English   中英

c# ASP.NET - API Controllers - Route 是否应该以“/”开头?

[英]c# ASP.NET - API Controllers - should Route start with "/" or not?

我有一个补救问题。 在我们的代码库中,我看到了不同的 API 控制器,有时,开发人员使用以“/”开头的路由,而其他时候则不是。

据我所知,无论端点是否以“/”开头,它们都可以通过同一个 URI 发现

 https://localhost:123/nameofcontroller

示例 C# 代码:

    [Route("/widgets/tools/calc")]

或 [Route("widgets/tools/calc")]

有关系吗?

编辑 1

所以经过一些额外的阅读之后,似乎我们正在使用属性路由......因为我们在控制器cs文件中定义了路由,如下所示:(如果我错了,请纠正我)

控制器1.cs

    [HttpGet]
    [Route("/widgets/{widgetID}/report

控制器2.cs

    [HttpGet]
    [Route("widgets/tools/calc

但我仍在尝试了解以“/”开头的路线与不以“/”开头的路线之间的差异。

阅读下面代码中的注释:

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 指定控制器路由它对 uri 没有影响。 指定控制器路由后,uris 将如下所示:

GET: /widgets/{widgetID}/report
GET: /controller2/widgets/tools/calc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM