代码之家  ›  专栏  ›  技术社区  ›  szozz

MVC Core 2.0未按约定搜索视图

  •  2
  • szozz  · 技术社区  · 7 年前

    /User/Index Works UserController 查找 /Views/User/Index.cshtml

    /User/Index?Page=2

    /User/Index/Page2 与一起失败

    InvalidOperationException:未找到视图“Index”。搜索了以下位置:/Views/Shared/Index。cshtml`

    /Views/User anymore .

    /用户/索引/第2页

    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的索引操作:

    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));
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   hMatoba    7 年前

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

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

    这是一个错误。ASP。NET Core MVC将解决以下版本。