Replace default parameter binding without default route values

I have an action in the controller, for example:

public ActionResult Index(string ssn) { } 

and default route values: {controller}/{action}/{id}

I do not want to use url like /Home/Index?ssn=1234 . I want to use as /Home/Index/1234 . But I also do not want to add new route values ​​for the ssn parameter (or a custom mediator).

Is there some kind of full attribute, for example [ActionName] , but for parameters?

Something like that:

 public ActionResult Index([ParameterBinding("id")] string ssn) { } 
+4
source share
1 answer

As Darin and Rumi mentioned - there are no built-in attributes, however, you can achieve the same effect (through several controllers / actions) using one new route using the restriction parameter RouteCollection.MapRoute on one route.

In the following route configuration, the "SSN" route is applied to the Foo or Bar controller, any other controller will follow the default route.

 routes.MapRoute( name: "SSN", url: "{controller}/{action}/{ssn}", defaults: new { controller = "Foo", action = "Index" }, constraints: new { controller = "(Foo|Bar)", action = "Index" } ); // default route routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 

Edit: Alternatively, you can use the ActionParameterAlias library, which seems to support what you originally requested.

0
source

Source: https://habr.com/ru/post/1502805/


All Articles