Reputation: 2139
Is it possible to have optional parameter in Url routing while using asp.net 4.0?
I mean i just pass value for one parameter and skip value for another parameter?
Thanks.
Upvotes: 1
Views: 1735
Reputation: 11
I can not give you an answer (I am still searching as well) but maybe a workaround. As far as I understand your question you want to be able to do the following:
a) my-pets.com/dog/brown/long/good-temper
b) my-pets.com/dog///mean-barker
c) my-pets.com/dog/black/
The user would be able to leave out a parameter (in the example b), the color of the dog and the kind of fur (short or long)
My workaround does the following:
string furType = Page.RouteData.Values["furType"] == null ? "-" : Page.RouteData.Values["furType"].ToString();
This allows the following route
In my code I use it to filter values from the database
var pet = new Pet();
pet.getPets(pet.filter(petType="dog", petTemper="mean-barker");
Upvotes: 1
Reputation: 21112
routes.MapPageRoute(
"ProductsBrowse",
"browse/{BrowseBy}/{Category}",
"~/Pages/Products/Browse.aspx",
false,
new RouteValueDictionary { { "Category", string.Empty } }
);
asp.net webforms routing: optional parameters
Upvotes: 0