Hi How do I map the url ../Companies/Results/value/id when both the parameters are optional?
Companies is the controller, Results is the action, value and id are optional parameters. On my form is a textbox for value and a selectlist for an id. The user could select both or one of each to search by. Tried something like this but cant handle when one of the optional parameters, say value, is missing such as ../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
);
You can make a URI parameter optional by adding a question mark to the route parameter. If a route parameter is optional, you must define a default value for the method parameter.
C# Multiple Optional ParametersIf you want to define multiple optional parameters, then the method declaration must be as shown below. The defined GetDetails() method can be accessed as shown below. GetDetails(1); GetDetails(1, "Rohini");
Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.
We can make a route parameter optional by adding “?” to it. The only gimmick is while declaring a route parameter as optional, we must specify a consequent default value for it: [HttpGet("GetById/{id?}")] In this case, we assign 1 as a default value for the id parameter in the GetById action method.
You can't have a route that has two optional parameters, only the last parameter can be optional precisely because of the problem you describe. I suggest that you have a default parameter for value
, like byid and use this when the person selects a profession.
I assume that you're constructing the URL via javascript, since using a GET form action would result in the parameter names being added to the URL. In this case, when the textbox is empty simply insert the default byid.
Update your route to include the default so any URLs that you generate will work. See Phil Haack's blog post on 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
);
Thanks guys, just discovered route constraints for integer. And so fiddling around with some combinations of routes it seems to 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
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With