分页和页面冲突-ASP.Net Core Razor Pages与MVC控制器方法分页冲突 [英] Pagination and Pages conflict - ASP.Net Core Razor Pages conflicts with MVC controller method pagination

查看:550
本文介绍了分页和页面冲突-ASP.Net Core Razor Pages与MVC控制器方法分页冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有Angular 6 spa项目的ASP.Net Core 2.1中,我使用此AccountController.cs方法返回用户的分页列表:

In an ASP.Net Core 2.1 with Angular 6 spa project, I return a paginated list of users using this AccountController.cs method:

[HttpGet("users")]
[Produces(typeof(List<UserViewModel>))]
public async Task<IActionResult> GetUsers()
{
    return await GetUsers(-1, -1);
}

[HttpGet("users/{page:int}/{pageSize:int}")]             <---- conflicts here
[Produces(typeof(List<UserViewModel>))]
public async Task<IActionResult> GetUsers(int page, int pageSize)
{
    var users = await _accountManager.GetUsersLoadRelatedAsync(page, pageSize);
    return Ok(Mapper.Map<List<UserViewModel>>(users));
}

在startup.cs中,我正在使用MVC,如下所示:

In startup.cs I am using MVC as follows:

app.UseMvc(routes =>
{
    routes.MapRoute("blog", "blog/{*article}", defaults: new { controller = "Blog", action = "Article" });
    routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

如上所述的代码可以正常工作,但是当我想在项目中开始使用Razor Pages时,我创建了一个新的标准Razor Pages项目模板并将其Pages文件夹添加到当前项目中,运行我收到错误的项目,如果再次移出Pages文件夹,则该项目开始运行而没有错误.

Code as described above works fine, but as I want to start using Razor Pages in my project, I created a new standard Razor Pages project template and added its Pages folder to the current project, but as soon as I do that and run the project I get error, if I move out again the Pages folder, the project start running without errors.

这是错误描述:

RouteCreationException:属性路由信息发生以下错误:

RouteCreationException: The following errors occurred with attribute routing information:

操作:'MyApiProject.Controllers.AccountController.GetUsers (MyApiProject)'

For action: 'MyApiProject.Controllers.AccountController.GetUsers (MyApiProject)'

错误:属性路由 'api/Account/users/{page:int}/{pageSize:int}'不能包含 参数名为"{page}".在路线模板中使用"[页面]"进行插入 值.

Error: The attribute route 'api/Account/users/{page:int}/{pageSize:int}' cannot contain a parameter named '{page}'. Use '[page]' in the route template to insert the value ''.

方法代码无需更改,因为它可以与现有的Angular spa一起正常工作.那么解决方法是什么?

The Method code hasn't to be changed since it is working just fine with the existing Angular spa. So what's the workaround ?

推荐答案

在使用ASP.NET Core MVC剃刀页面时,{page}成为有效的保留字,就像{controller}{action}是在使用传统MVC控制器和操作时使用的.为了解决此问题,您可以简单地使用其他内容来表示执行分页时所需的页码.例如:

When using ASP.NET Core MVC Razor Pages, {page} becomes what is effectively a reserved word, just as {controller} and {action} are when working with traditional MVC controllers and actions. In order to work around this, you can simply use something else to represent the page number desired when performing pagination. For example:

[HttpGet("users/{pageNumber:int}/{pageSize:int}")]
[Produces(typeof(List<UserViewModel>))]
public async Task<IActionResult> GetUsers(int pageNumber, int pageSize)
{
    var users = await _accountManager.GetUsersLoadRelatedAsync(pageNumber, pageSize);
    return Ok(Mapper.Map<List<UserViewModel>>(users));
}

因为您以前使用的是{page}作为路由值,所以将其切换为{pageNumber}不会对调用应用程序产生任何影响,因为它们没有直接使用page.如果这是一个查询字符串参数,那将是另一回事了.

Because you were previously using {page} as a route-value, switching this to {pageNumber} will have no affect on calling applications as they did not use page directly. Had this been a query-string parameter, that would be a different story.

这篇关于分页和页面冲突-ASP.Net Core Razor Pages与MVC控制器方法分页冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆