Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing ASP.NET Core 5 - default value of id always returns 0

I have a project in ASP.NET Core 5, what I need is quite simple. I want to indicate in the id value a default int value in routing. However, when I trace the controller, it always returns the id as 0 or null.

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

this is my Start.up file

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

        });

As you can see, I expect when the https://localhost:xxx/podcast/seccion/historias is entered the Seccion() action method is called with id set to 300 by default. But the id always arrives 0.

like image 725
Daniel Lazarte Avatar asked Sep 06 '21 03:09

Daniel Lazarte


People also ask

How do I set the default route in .NET Core Web API?

Right click your web project -> Select Properties -> Select the Debug tab on the left -> Then edit the 'Launch Url' field to set your own default launch url.

How does core routing work in asp net?

Routing uses a pair of middleware, registered by UseRouting and UseEndpoints: UseRouting adds route matching to the middleware pipeline. This middleware looks at the set of endpoints defined in the app, and selects the best match based on the request. UseEndpoints adds endpoint execution to the middleware pipeline.

How do I enable routing in .NET Core?

Inside the ConfigureRoute method, you can configure your routes; you can see that this method has to take a parameter of type IRouteBuilder. The goal of routing is to describe the rules that ASP.NET Core MVC will use to process an HTTP request and find a controller that can respond to that request.

How would you setup the default route using endpoints?

UseEndpoints(endpoints => { endpoints. MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Inside the call to UseEndpoints() , we use the MapControllerRoute() method to create a route by giving the name default .


2 Answers

Your problem caused by wrong routes registration order (conventional routing is order-dependent). It is important that you add "historias" route before the Default one. Routes are processed in the order they are listed. So, define routes in the following order:

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

And the "historias" route template does not include the controller name. Therefore it is necessary to use the following URL: https://localhost:port/seccion or https://localhost:port/seccion/[some_number_here].

Pay attention! When you are using http://localhost:port/podcast/seccion the "default" route template is engaged and therefore the id is 0.

But, if you will define the default id value in the action method like public IActionResult Seccion(int id=300) the following URL's https://localhost:port/podcast/seccion or https://localhost:port/podcast/seccion/[some_number_here] will properly work too.

For more information see Set up conventional route

like image 71
Victor Avatar answered Oct 23 '22 03:10

Victor


you should set your default value in Action

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

when call https://localhost:xxx/podcast/seccion id is 300 and when call https://localhost:xxx/podcast/seccion/1 id is 1

for more information read this

like image 28
Nima Talebi Avatar answered Oct 23 '22 03:10

Nima Talebi