user765674
user765674

Reputation:

URL Routing in MVC 3

my current url is something like this => http://localhost:4330/Restaurants/?Location=Manchester&Cuisine=0&NetProfit=0&Turnover=0&MaxPrice=120000&SortPriceBy=Low&Page=0

i want it to make something like this => http://localhost:4330/Restaurants/Manchester/?Cuisine=Chinese&MaxPrice=120000

Where Param Query string that doesnt have values (0) will not be included on query string URL Is it possible?

Upvotes: 2

Views: 1151

Answers (2)

Evgeniy Labunskiy
Evgeniy Labunskiy

Reputation: 2042

UPDATED

stringAdd this to Global.asax routes

            routes.MapRoute(
                "Name of route", // Route name
                "Restaurants/{cityid}/", // URL with parameters
                new { controller = "Restaurants", action = "Index" } // Parameter defaults
            );

This is controller:

public ActionResult Index(string city, int cuisine = 0, int ChineseMaxPrice=0)
{
  Return View();
}

Like int cuisine = 0 - this set the default value to parameter if this parameter is not set in querystring

string city - is a parameter that should be in string (not optional)

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

Try adding add the corresponding route:

routes.MapRoute(
    "Restaurants",
    "Restaurants/{city}",
    new { controller = "Restaurants", action = "Index", city = UrlParameter.Optional }
);

which would map to the Index action on the Restaurants controller:

public ActionResult Index(string city) 
{
    ...
}

Upvotes: 1

Related Questions