![](/img/trans.png)
[英]Microsoft.AspNetCore.Routing.RouteCreationException: 'An error occurred while creating the route with name 'default' and template
[英]RouteCreationException: The following errors occurred with attribute routing information
您好,我在运行API时收到此错误:“错误:属性路由'api / Book / {page} / {itemsPerPage}'不能包含名为'{page}'的参数。在路由模板中使用'[page]'插入值“”。
[HttpGet("{page}/{itemsPerPage}", Name = "GetBookWithPagination")]
[ProducesResponseType(404)]
[ProducesResponseType(400)]
[ProducesResponseType(200, Type = typeof(PaginationResult<Book>))]
public async Task<ActionResult<PaginationResult<Book>>> Get(int page, int itemsPerPage, string filter)
{
try
{
var result = new PaginationResult<Book>();
result = bookRepo.RetrieveBookWithPagination(page, itemsPerPage, filter);
return result;
}
catch (Exception)
{
return BadRequest();
}
}
删除此代码块时,API工作正常
使用ASP.NET Core MVC剃刀页面时,{page}实际上是保留字,就像使用传统MVC控制器和动作时{controller}和{action}一样。 为了解决此问题,您可以简单地使用其他内容来表示执行分页时所需的页码。
例如:
[HttpGet("{pageIndex}/{itemsPerPage}", Name = "GetBookWithPagination")]
[ProducesResponseType(404)]
[ProducesResponseType(400)]
[ProducesResponseType(200, Type = typeof(PaginationResult<Book>))]
public async Task<ActionResult<PaginationResult<Book>>> Get(int page, int itemsPerPage, string filter)
{
try
{
var result = new PaginationResult<Book>();
result = bookRepo.RetrieveBookWithPagination(page, itemsPerPage, filter);
return result;
}
catch (Exception)
{
return BadRequest();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.