szozz
szozz

Reputation: 145

MVC Core 2.0 Not Searching for View by Convention

I converted an application from MVC Core 1 to 2. Everything is working except that now Controller Actions that support pagination fail to search the Views path by convention when the Page parameter is supplied. For instance:

/User/Index Works, The UserController finds /Views/User/Index.cshtml

/User/Index?Page=2 Works.

/User/Index/Page2 Fails with

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

Notice not searching /Views/User anymore.

Routing seems to be working since the page links are changed by the tag helpers to /User/Index/Page2 format. If I remove the pagination route the urls revert to the querystring version. Here are my routes:

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

And the Index Action of UserController:

public async Task<IActionResult> Index(int page = 1) => 
  View( await GetIndexModel(page));

Pagination URLs work if I specify the entire path to the View:

public async Task<IActionResult> Index(int page = 1) =>
  View("~/Views/User/Index.cshtml", await GetIndexModel(page));

I use the same pagination pattern in many controllers and now they are all broken in the same way. Pagination URLs and View by convention were working in Core 1, any idea why Core 2 would not search for a View by convention in certain circumstances? What does the page parameter have to do with Controller View searches? How can I force the Controller to search for the View by convention every time?

Upvotes: 2

Views: 636

Answers (1)

hMatoba
hMatoba

Reputation: 547

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

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

It's a bug. ASP.NET Core MVC following versions will been soloved. Temporary solution is to replace from "{page}" to another name.

Upvotes: 1

Related Questions