Yaroslav Yakovlev
Yaroslav Yakovlev

Reputation: 6483

Add route for asp.net mvc 3

I need to be able to hande routes like this: appdomain/city/City-state, so in case somebody used appdomain/Washington/Washington-DC he retrieves proper info from proper controller action. For now can`t get what controller and action it should be to get this url and handle it properly.

To clear it a bit, there`s like no controller and action, but 2 parameters instead of them.

Upvotes: 2

Views: 3123

Answers (2)

balexandre
balexandre

Reputation: 75073

Why not adding a little help from a fixed path, like Show-City

routes.MapRoute(
    "CityAndState",
    "Show-City/{city}/{state}",
    new { controller = "Cities", action = "Index", id = UrlParameter.Optional }
);

this will never interfere with your existing routes, and then you can use:

http://domain.com/Show-City/New York/NY

at your Index Action inside the Cities Controller you would have something like:~

public class CitiesController : Controller
{
    public ActionResult Index(string city, string state)
    {
        // use city and state variables here

        return View();
    }
}

Upvotes: 3

marcind
marcind

Reputation: 53183

Try this:

routes.MapRoute("Foo", "{state}/{city}",
    new { controller = "ControllerName", action = "ActionName" });

and in your class you'd have:

public class ControllerNameController : Controller {
    public ActionResult ActionName(string state, string city) {
         ...
    }
}

Upvotes: 1

Related Questions