简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM