undefined /swagger/v1/swagger.json if i add [Route] attribute

huangapple go评论66阅读模式
英文:

undefined /swagger/v1/swagger.json if i add [Route] attribute

问题

我有我的控制器和我的操作方法GetAvailabilitiesForPorducts。如果我从[HttpGet]属性中移除Name属性,这将正常工作。即使我移除[Route]属性,一切也能正常工作。

    [ApiController]
    [Route("api/products")]
    public class ProductsController : ControllerBase
    {

        [Route("availability")]
        [HttpGet(Name = "GetAvailabilitiesForPorducts")]
        public async Task<ActionResult<IEnumerable<AvailabilityDTO>>> GetAvailabilitiesForPorducts([FromQuery] AvailabilityResourceParams availabilityResourceParams)
        { ... }

我需要这两个属性。我已经检查过,我没有其他具有相同名称的操作。

英文:

I have my controller an my action method GetAvailabilitiesForPorducts. If i remove the Name property from the [HttpGet] attribute, this works fine. Everithing works even if i remove the [Route] attribute.

    [ApiController]
    [Route(&quot;api/products&quot;)]
    public class ProductsController : ControllerBase
    {

        [Route(&quot;availability&quot;)]
        [HttpGet(Name = &quot;GetAvailabilitiesForPorducts&quot;)]
        public async Task&lt;ActionResult&lt;IEnumerable&lt;AvailabilityDTO&gt;&gt;&gt; GetAvailabilitiesForPorducts([FromQuery] AvailabilityResourceParams availabilityResourceParams)
        { ... }

I need Both attributes. I already checked, i do not have other actions with same name.

答案1

得分: 0

你在调试时可以在地址栏中输入 https://localhost:xxxx/swagger/v1/swagger.json 来检查错误。

在我的一侧,错误如下:

undefined /swagger/v1/swagger.json if i add [Route] attribute

根据您的设置,注册了两个路由:

默认路由:api/products/availability

新的路由模式命名为 GetAvailabilitiesForPorductsapi/products

undefined /swagger/v1/swagger.json if i add [Route] attribute

实际上,您可以直接在 HttpGet 属性的构造函数中编写路由模式,如下所示:

[ApiController]
[Route("api/products")]
public class MyApiController : ControllerBase
{
    //[Route("availability")]
    [HttpGet("availability", Name = "GetAvailabilitiesForPorducts")]
    public IActionResult SomeAction()
    {
        return Ok();
    }
}

结果:
undefined /swagger/v1/swagger.json if i add [Route] attribute

英文:

You could input https://localhost:xxxx/swagger/v1/swagger.json in address bar when you debug to check the error

On myside, the error :

undefined /swagger/v1/swagger.json if i add [Route] attribute

With your settings,two routes were registed:

default route : api/products/availability

a new route parttern name of GetAvailabilitiesForPorducts : api/products

undefined /swagger/v1/swagger.json if i add [Route] attribute

In fact,you could write your route parttern directly in the constructor of HttpGet Attribute ,fixed with

[ApiController]
    [Route(&quot;api/products&quot;)]
    public class MyApiController : ControllerBase
    {

        //[Route(&quot;availability&quot;)]
        [HttpGet(&quot;availability&quot;, Name = &quot;GetAvailabilitiesForPorducts&quot;)]
        public IActionResult SomeAction()
        {
            return Ok();
        }
    }

Result:
undefined /swagger/v1/swagger.json if i add [Route] attribute

huangapple
  • 本文由 发表于 2023年7月6日 15:20:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定