【问题标题】:MVC Core 2.0 Not Searching for View by ConventionMVC Core 2.0 不按约定搜索视图
【发布时间】: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


    【解决方案1】:

    https://github.com/aspnet/Mvc/issues/6706

    https://github.com/aspnet/Mvc/issues/6680

    这是一个错误。 ASP.NET Core MVC 以下版本将被解除。 临时解决方案是将“{page}”替换为另一个名称。

    【讨论】:

    • 我将注释掉分页路由并回退到使用查询字符串 URL,因为更改参数名称 Page 涉及更新所有操作签名。这条路线实际上是为了让 URL “更漂亮”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2019-01-19
    • 2021-03-10
    相关资源
    最近更新 更多