I want to configure a route with optional flags.
E.g. I want to be able to call the products page and send optional filters (flags) for offers and in stock options. If the flags are NOT specified then all products should be returned.
http://localhost/products/onlyOnOffer
http://localhost/products/onlyInStock
http://localhost/products/onlyInStock/onlyOnOffer
[AcceptVerbs(HttpVerbs.Get)] public ActionResult GetProducts(bool onlyInStock, bool onlyOnOffer) { //... }
How would I configure the route? Is it even possible in MVC 1.0? What about MVC 2.0.
1:
I see 2 ways:
1) Set up 4 different Actions:
[AcceptVerbs(HttpVerbs.Get)] public ActionResult Index() { //... } [AcceptVerbs(HttpVerbs.Get)] public ActionResult IndexOnOffer() { //... } [AcceptVerbs(HttpVerbs.Get)] public ActionResult IndexInStock() { //... } [AcceptVerbs(HttpVerbs.Get)] public ActionResult IndexInStockAndOnOffer() { //... }
2) Send in the 2 parameters in the query string: http://localhost/products/?onlyInStock=false&onlyOnOffer=true
[AcceptVerbs(HttpVerbs.Get)] public ActionResult GetProducts(bool? onlyInStock, bool? onlyOnOffer) { //... }
2:
You're best off using query strings for this. Note that the path is supposed to describe a resource - in this case, the resource that the user wants to get is the products page itself. Attributes like "only in stock" and "only on offer" are modifiers which change only how the resource is displayed to the end user; they don't change the fact that the resource you're getting is the products page.