asp.net core2路由问题

问题描述 投票:3回答:1

我正在测试.net core2并在我的routesStatrtUp方法中跟随configure

app.UseMvc(routes => {
    routes.MapRoute(
            name: "pagination",
            template: "Products/Page{page}",
            defaults: new { controller = "Product", action = "List" });

        routes.MapRoute(
            name: "default",
            template: "{controller=Product}/{action=List}/{id?}");
});

代码来自Pro ASP.NET Core MVC 6th Edition第8章,本书适用于Asp.net Core1。

Url http://localhost:65000/效果很好,但http://localhost:65000/Products/Page2不起作用。

网址http://localhost:65000/正在调用ProductControllerList动作,但http://localhost:65000/Products/Page2给了我这个例外:

InvalidOperationException: The view 'List' was not found. The following locations were searched: /Views/Shared/List.cshtml

显然,/Views/Product/文件夹不会搜索List。我的路线有什么问题?我正在使用的新项目的模板是带有身份验证的Web Application(Model-View-Controller)Individula User Accounts

编辑

添加了控制器代码,这只是我之前提到的书中的示例代码。

public class ProductController : Controller {

    private IProductRepository repository;
    int PageSize = 4;

    public ProductController(IProductRepository repo) {
        repository = repo;
    }


    public ViewResult List(int page = 1) => View(
        new ProductsListViewModel {
            Products = repository.Products
                .OrderBy(x => x.Name)
                .Skip(PageSize * (page - 1))
                .Take(PageSize),
            PagingInfo = new PagingInfo {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = repository.Products.Count()
            }
        }
    );
}
c# asp.net-core-2.0
1个回答
3
投票

我创立了解决方案。问题是Microsoft.AspNetCore.All 2.0.1中的一个错误,并将其更新为2.0.3修复了该错误。它与asp.net core 2中的新Razor Pages功能相关,并在路由模板中使用Page

有关更多分辨率,请参阅此链接GitHub aspnet/Mvc Issue 6660

© www.soinside.com 2019 - 2024. All rights reserved.