ASP.NET Core 2 路由问题

3

我正在使用 .net core2 进行测试,我的 StatrtUpconfigure 方法中有以下的 routes

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

这段代码来自于《Pro ASP.NET Core MVC 6th Edition》第8章,这本书是为Asp.net Core1编写的。

网址http://localhost:65000/运行良好,但http://localhost:65000/Products/Page2不能正常工作。

http://localhost:65000/调用了ProductControllerList操作,但http://localhost:65000/Products/Page2给出了以下异常:

InvalidOperationException:未找到视图'List'。已搜索以下位置: /Views/Shared/List.cshtml

显然,/Views/Product/文件夹没有搜索到List。我的路由有什么问题?我正在使用的新项目模板是Web应用程序(模型-视图-控制器),带有身份验证:单独用户帐户

编辑

添加了控制器代码,这只是我之前提到的书中的示例代码。

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

你能发布一些你的产品控制器吗? - Dan Soper
这很简单。我正在编辑帖子以添加它。 - ahmad molaie
你的视图文件路径为 /Views/Product/List.cshtml?大小写是否匹配? - Dan Soper
好的。如果你正在访问Action方法,那么你的路由不是问题所在。项目设置或Program.cs/Startup.cs中的某些内容可能存在问题。 - Dan Soper
@DanSoper 看看答案,我在 GitHub 上找到了解决方案(和/或问题)。没有任何问题! - ahmad molaie
显示剩余2条评论
1个回答

3

我找到了解决方案。问题是Microsoft.AspNetCore.All 2.0.1中的一个错误,将其更新为2.0.3可以修复该错误。这与asp.net core 2中的新Razor Pages功能有关,并且在路由模板中使用Page。

查看此链接以获取更多解决方案:GitHub aspnet/Mvc Issue 6660


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接