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

4k views Asked by At

After setting up pagination for my web app, I decided to change the url so that it looks nicer. Everything worked perfectly until I decided to add a route which gives me the following error after I change page

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

And the routing

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

There doesn't seem to be any typo and I've checked thrice that the controllers and views are in the correct path and have the correct methods. What may be the problem here? Its not searching in the right folder for the view

1

There are 1 answers

0
Win On BEST ANSWER

Could you make page optional and try again? If page is null inside Books action method, then you could throw a custom error inside the method.

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

Or use attribute routing.

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