【发布时间】:2017-09-27 22:04:35
【问题描述】:
我将应用程序从 MVC Core 1 转换为 2。除了现在支持分页的控制器操作在提供 Page 参数时无法按约定搜索视图路径之外,一切正常。例如:
/User/Index 有效,UserController 找到 /Views/User/Index.cshtml
/User/Index?Page=2 工作。
/User/Index/Page2 失败
InvalidOperationException:未找到视图“索引”。搜索了以下位置:/Views/Shared/Index.cshtml`
注意不要搜索/Views/User anymore。
由于标签助手将页面链接更改为/User/Index/Page2 格式,因此路由似乎工作正常。如果我删除分页路由,则 url 将恢复为查询字符串版本。这是我的路线:
app.UseMvc(routes => {
routes.MapRoute(
name: "pagination",
template: "{controller}/{action}/Page{page}",
defaults: new { action = "Index"}
);
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}"
);
});
以及UserController的Index Action:
public async Task<IActionResult> Index(int page = 1) =>
View( await GetIndexModel(page));
如果我指定视图的完整路径,分页 URL 将起作用:
public async Task<IActionResult> Index(int page = 1) =>
View("~/Views/User/Index.cshtml", await GetIndexModel(page));
我在许多控制器中使用相同的分页模式,现在它们都以相同的方式损坏。分页 URL 和按约定查看视图在 Core 1 中工作,知道为什么 Core 2 在某些情况下不会按约定搜索视图吗?页面参数与控制器视图搜索有什么关系?如何强制控制器每次都按约定搜索视图?
【问题讨论】:
标签: asp.net-mvc asp.net-core-mvc