Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mvc: route config order routes

i have added a custom route like this

        routes.MapRoute(
          name: "Default",
          url: "{coutry}/{lang}/{controller}/{action}",
          defaults: new { controller = "Home", action = "Index" }
        );

now im getting some problems when i try to call a method from one controller, this was working good before add the new route

<a id="someId" class="link-button" href="../Documents/Create"><span>Create</span></a>

now the only way i can achieve that is with something like href="EN/us/Documents/Create"

is there a way for keeping the custom route for my client side, and still keeping the href="../Documents/Create"> way for my admin side, it´s because i have several functionality in admin side developed but now i have to include that custom route for client side. Thank you so much.

There are my routes now

 routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );

routes.MapRoute(
            name: "CustomRoute",
            url: "{country}/{lang}/{controller}/{action}",
            defaults: new { controller = "Test", action = "Index" }
        );

but i can only access to CustomRoute with /ES/es/Test/Index ... why is not taking default values ?

like image 623
Steve Avatar asked Nov 28 '25 12:11

Steve


2 Answers

You just need to declare your custom route after the default route:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index",
        id = UrlParameter.Optional }
);
// SomeOther = someothercontroller
routes.MapRoute(
    name: "CustomRoute",
    url: "{coutry}/{lang}/{controller}/{action}",
    defaults: new { controller = "SomeOther", action = "Index" }
);
like image 78
von v. Avatar answered Dec 01 '25 07:12

von v.


You have replaced the default RouteConfig to the new configuration and it does match a Url in this {coutry}/{lang}/{controller}/{action} format.

If you want to accept the ../Documents/Create Url You have to add the default RouteConfig at the end.

routes.MapRoute(
  name: "CustomRoute",
  url: "{coutry}/{lang}/{controller}/{action}",
  defaults: new { controller = "Documents", action = "Index" }
);

routes.MapRoute(
  name: "Default",
  url: "{controller}/{action}/{id}",
  defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Also in the anchor tag <a id="someId" class="link-button" href="../Documents/Create"><span>Create</span></a> instead of hardcoding the href you can write the like following.

<a id="someId" class="link-button" href="@Url.Action("Create","Documents")><span>Create</span></a>

like image 38
Saanch Avatar answered Dec 01 '25 08:12

Saanch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!