InvalidOperationException: 添加路由后未找到视图 'ViewName'

InvalidOperationException: The view 'ViewName' was not found after adding route

为我的网络应用程序设置分页后,我决定更改 url 以使其看起来更漂亮。一切正常,直到我决定添加一条路线,在我更改页面后出现以下错误

"InvalidOperationException: The view 'Books' was not found. The following locations were searched: /Views/Shared/Books.cshtml"

和路由

routes.MapRoute
(
     name: "pagination",
     template: "Books/Page/{page}",
     defaults: new {Controller = "Core", action = "Books"}
);

似乎没有任何拼写错误,我已经检查了三次控制器和视图是否在正确的路径中并且具有正确的方法。 这可能是什么问题?它没有在正确的文件夹中搜索视图

能否将 page 设为可选,然后重试? 如果 Books 操作方法中的页面为空,那么您可以在该方法中抛出自定义错误。

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "pagination",
        template: "Books/Page/{page?}"
        defaults: new { controller = "Core", action = "Books"});
});

或者使用属性路由。

public class CoreController : Controller
{
   [HttpGet("/Books/Page/{page?}")]
   public IActionResult Books(int? page) { ... }
}