ASP.NET CORE2路由问题



我正在使用.net core2进行测试,并且在我的 StatrtUp's configure方法中有 routes

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行不通。

URL 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()
            }
        }
    );
}

我找到了解决方案。问题是Microsoft.aspnetcore.ly 2.0.1中的一个错误,并将其更新为2.0.3修复了错误。它与ASP.NET Core 2中的新剃刀页面以及路由模板中的页面有关。

请参阅此链接以获取更多分辨率GitHub ASPNET/MVC问题6660

最新更新