我正在用.net core2进行测试,并在StatrtUp的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/正在调用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()
}
}
);
}发布于 2017-12-22 21:34:02
我找到了解决办法。这个问题是Microsoft.AspNetCore.All 2.0.1中的一个bug,并将其更新到2.0.3修复了该错误。它与asp.net核心2中新的Razor特性以及在路由模板中使用Page有关。
有关更多分辨率的GitHub aspnet/Mvc问题6660,请参阅此链接
https://stackoverflow.com/questions/47947471
复制相似问题