英文:
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("api/products")]
public class ProductsController : ControllerBase
{
[Route("availability")]
[HttpGet(Name = "GetAvailabilitiesForPorducts")]
public async Task<ActionResult<IEnumerable<AvailabilityDTO>>> 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
来检查错误。
在我的一侧,错误如下:
根据您的设置,注册了两个路由:
默认路由:api/products/availability
新的路由模式命名为 GetAvailabilitiesForPorducts
:api/products
实际上,您可以直接在 HttpGet
属性的构造函数中编写路由模式,如下所示:
[ApiController]
[Route("api/products")]
public class MyApiController : ControllerBase
{
//[Route("availability")]
[HttpGet("availability", Name = "GetAvailabilitiesForPorducts")]
public IActionResult SomeAction()
{
return Ok();
}
}
英文:
You could input https://localhost:xxxx/swagger/v1/swagger.json
in address bar when you debug to check the error
On myside, the error :
With your settings,two routes were registed:
default route : api/products/availability
a new route parttern name of GetAvailabilitiesForPorducts
: api/products
In fact,you could write your route parttern directly in the constructor of HttpGet
Attribute ,fixed with
[ApiController]
[Route("api/products")]
public class MyApiController : ControllerBase
{
//[Route("availability")]
[HttpGet("availability", Name = "GetAvailabilitiesForPorducts")]
public IActionResult SomeAction()
{
return Ok();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论