Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net core2 routing issue

I'm testing with .net core2 and have following routes in my StatrtUp's configure method:

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

        routes.MapRoute(
            name: "default",
            template: "{controller=Product}/{action=List}/{id?}");
});

the code is from Pro ASP.NET Core MVC 6th Edition Chapter 8, and the book is for Asp.net Core1.

Url http://localhost:65000/ works great, but http://localhost:65000/Products/Page2 does not works.

the url http://localhost:65000/ is calling ProductController's List action, but http://localhost:65000/Products/Page2 gives me this exception:

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

obviously, /Views/Product/ folder is not searched for List. what is the problem with my route? the template for new project i'm using is Web Application(Model-View-Controller) with Authentication : Individula User Accounts

Edit

Added Controller code, this is just sample code from book i mentioned earlier.

public class ProductController : Controller {

    private IProductRepository repository;
    int PageSize = 4;

    public ProductController(IProductRepository repo) {
        repository = repo;
    }


    public ViewResult List(int page = 1) => View(
        new ProductsListViewModel {
            Products = repository.Products
                .OrderBy(x => x.Name)
                .Skip(PageSize * (page - 1))
                .Take(PageSize),
            PagingInfo = new PagingInfo {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = repository.Products.Count()
            }
        }
    );
}
like image 613
ahmad molaie Avatar asked Jul 17 '26 12:07

ahmad molaie


1 Answers

I found the solution. The problem was a bug in Microsoft.AspNetCore.All 2.0.1 and updating it to 2.0.3 fixed the bug. It was related to new Razor Pages feature in asp.net core 2 and using Page in route template.

See this link for more resolution GitHub aspnet/Mvc Issue 6660

like image 147
ahmad molaie Avatar answered Jul 20 '26 00:07

ahmad molaie