Asp mvc routing with two optional parameters

Hi How to match url ../Companies/Results/value/id when both parameters are optional?

Companies are the controller, results are the action, value and identifier are optional parameters. In my form, there is a text box for the value and selectlist for id. The user can select one or each of them to search. I tried something like this, but could not handle it when one of the optional parameters is missing, for example, value./Companies/Results//id

routes.MapRoute( "Company+Profession", // Route name "{action}/{value}/{profId}", // URL with parameters new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults ); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); 
+4
source share
3 answers

You do not have a route with two optional parameters, only the last parameter may be optional precisely because of the problem you are describing. I suggest you set the default parameter for value , for example byid , and use it when a person chooses a profession.

I assume that you are creating the url via javascript, as using the action of the GET form will add the parameter names to the url. In this case, when the text box is empty, just insert the default value byid .

Update your route to enable it by default so that all the URLs that you created will work. See Phil Haack 's blog post about this for an alternative way to handle generating URLs that have two "optional" parameters.

 // used when both parameters are specified routes.MapRoute( "Company+Profession", // Route name "{action}/{value}/{profId}", // URL with parameters new { controller = "Companies", action = "Index", value ="byid", profId = UrlParameter.Optional } // Parameter defaults ); 
+8
source

Thanks guys just discovered the route restrictions for the whole. And therefore, playing with some combination of routes, it seems that I work the way I want:

  routes.MapRoute( "Detail", // Route name "{action}/{value}", // URL with parameters new { controller = "Companies", action = "Detail" }, // Parameter defaults new { value = @"\d+" } //integer only ); routes.MapRoute( "Company + Profession", // Route name "{action}/{value}/{profId}", // URL with parameters new { controller = "Companies", action = "Results" }, // Parameter defaults new { profId = @"\d+" } //integer only ); routes.MapRoute( "Profession", // Route name "{action}/{profId}", // URL with parameters new { controller = "Companies", action = "Results"}, // Parameter defaults new {profId = @"\d+" } //integer only ); routes.MapRoute( "Company", // Route name "{action}/{value}", // URL with parameters new { controller = "Companies", action = "Results" } // Parameter defaults ); routes.MapRoute( "RootFolder", // Route name "{action}/{value}", // URL with parameters new { controller = "Companies", action = "Index", value = UrlParameter.Optional } // Parameter defaults ); 
+1
source

I'm not sure, since now I have no where to try, but here is my suggestion

 routes.MapRoute( "Company+Profession", // Route name "Companies/{action}/value-{value}/id-{profId}", // URL with parameters new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults ); 
0
source

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


All Articles