简体   繁体   English

asp.net 中的 app.UseMvc 路由不存在

[英]app.UseMvc routes in asp.net doesn't exists

I'm learning asp.net and I have a issue using routes in the startup file.我正在学习 asp.net 并且在启动文件中使用路由时遇到问题。 I need to change the url to do something like this我需要改变 url 来做这样的事情

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

But it said template parameter does not exists.但它说模板参数不存在。 I'm using Endpoints so how to do it with endpoints?我正在使用端点,那么如何使用端点呢?

My actual code looks like this我的实际代码如下所示

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Product}/{action=List}/{id?}");

    });

Thanks谢谢

I suggest an easier way by declaring your route above the action itself.我建议通过在操作本身之上声明您的路线来更简单的方法。

[Route("Products/Page/{productPage?}")]
public async Task<IActionResult> Products(string productPage) {
    return View();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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