【问题标题】:Routing ASP.NET Core 5 - default value of id always returns 0路由 ASP.NET Core 5 - id 的默认值始终返回 0
【发布时间】:2021-11-03 04:50:58
【问题描述】:

我有一个 ASP.NET Core 5 的项目,我需要的很简单。我想在 id 值中指明路由中的默认 int 值。但是,当我跟踪控制器时,它总是将 id 返回为 0null

public IActionResult Seccion(int id)
{
   
    return View();
}

这是我的 Start.up 文件

 app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=podcast}/{action=Index}/{id?}");

            endpoints.MapControllerRoute(
                        name: "historias",
                        pattern: "seccion/{id=300}",
                        defaults: new { controller = "Podcast", action = "Seccion" });

        });

如您所见,我希望在输入 https://localhost:xxx/podcast/seccion/historias 时调用 Seccion() 操作方法,默认情况下将 id 设置为 300。但是id 总是到达0

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core routes


    【解决方案1】:

    您的问题是由错误的路线注册顺序引起的(conventional routing is order-dependent)。 在默认路由之前添加 "historias" 路由很重要。路由按照它们列出的顺序进行处理。因此,请按以下顺序定义路由:

    app.UseEndpoints(endpoints =>
            {      
                endpoints.MapControllerRoute(
                            name: "historias",
                            pattern: "{controller}/seccion/historias/{id=300}",
                            defaults: new { controller = "Podcast", action = "Seccion" });
    
                endpoints.MapControllerRoute(
                            name: "default",
                            pattern: "{controller=podcast}/{action=Index}/{id?}");
            });
    

    "historias" 路由模板不包含控制器名称。因此需要使用以下 URL:https://localhost:port/seccionhttps://localhost:port/seccion/[some_number_here]

    注意!当您使用 http://localhost:port/podcast/seccion 时,"default" 路由模板已启用,因此 id0

    但是,如果您要在public IActionResult Seccion(int id=300) 之类的操作方法中定义默认的id 值,则以下URL 的https://localhost:port/podcast/seccionhttps://localhost:port/podcast/seccion/[some_number_here] 也可以正常工作。

    欲了解更多信息,请参阅Set up conventional route

    【讨论】:

    • 不,我想要的是 URL 仅在 localhost / podcast / section / stories 中是干净的,因此在内部传递值 300,但不将其放置在浏览器中。
    • 我试过了,改变了顺序,但它的 id 值一直为 0。 :(
    • @Daniel Lazarte:注意,如果您使用[Route(...)] 属性,它将获得高优先级。如果您不使用路由属性,它将起作用。您使用的究竟是什么 URL?如果您使用 https://localhost:port/podcast/seccion,那么您必须在操作方法中定义默认 id 值,例如 public IActionResult Seccion(int id=300)
    • @Daniel Lazarte:我对“historias”路线进行了更改。如果您输入https://localhost:port/podcast/seccion/historias URL,则将调用Seccion() 操作,默认id 等于300,操作方法定义为public IActionResult Seccion(int id),没有默认值。试试这个。
    • 优秀。这就是我一直在寻找的。我非常感谢您的支持。我不知道我的问题是否正确,但这就是我想要的。非常感谢和祝福
    【解决方案2】:

    你需要给 url 上的 ID 赋值,像这样:

    https://localhost:44394/podcast/seccion/300

    结果:

    更多相关内容请参考官方文档:

    Routing to controller actions in ASP.NET Core

    【讨论】:

    • 不,我想要的是 URL 仅在 localhost / podcast / section / stories 中是干净的,因此在内部传递值 300,但不将其放置在浏览器中。
    【解决方案3】:

    试试这样:-

    你的控制器:-

    [HttpGet]
    [Route("[action]/{id}")
    public IActionResult Seccion(int id)
    {
       
        return View();
    }
    

    当您调用https://localhost:xxx/podcast/seccion/id 时,您会发现您的输出与预期一样,并且您的 id 默认为 300。或者您可以简单地将其用作默认 id 值:- public IActionResult Seccion(int id ?? 300)

    【讨论】:

    • 不,我想要的是 URL 仅在 localhost / podcast / section / stories 中是干净的,因此在内部传递值 300,但不将其放置在浏览器中。
    【解决方案4】:

    你应该在 Action 中设置你的默认值

    public IActionResult Seccion(int id = 300)
    {
       
        return View();
    }
    

    当调用 https://localhost:xxx/podcast/seccion id300 当调用https://localhost:xxx/podcast/seccion/1 id1

    更多信息read this

    【讨论】:

    • 不,我想要的是 URL 仅在 localhost / podcast / section / stories 中是干净的,因此在内部传递值 300,但不将其放置在浏览器中。
    猜你喜欢
    • 2021-10-17
    • 2019-02-07
    • 1970-01-01
    • 2020-05-31
    • 2017-07-08
    • 2015-07-12
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多