我正在测试.net core2并在我的routes
的StatrtUp
方法中跟随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/
正在调用ProductController
的List
动作,但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.All
2.0.1
中的一个错误,并将其更新为2.0.3
修复了该错误。它与asp.net core 2中的新Razor Pages
功能相关,并在路由模板中使用Page
。
有关更多分辨率,请参阅此链接GitHub aspnet/Mvc Issue 6660