Search code examples
asp.net-mvc-3routesurl-mapping

How Do I get City State Zip in MVC 3 URL Route without writing a controller for every state and actions for each city


I have the need to have the urls in my bosses application look like:

http://domain.com/Texas/Dallas/72701

However, I don't want to write a controller for every state and an action for every city.

routes.MapRoute(
"DrillDown", // Route name
"{controller}/{action}/{ZipId}", 
new { controller = "State", action = "City", ZipId = UrlParameter.Optional} // Parameter     defaults

Can someone help me write a pattern for the routes that will accept State/City/Zip without destroying the ability for me to have regular routes with controller/Action/Param ?

Looking all over and can't find any direction.


Solution

  • If you are able to add one more path segment to the domain path you could get this to work pretty easily

    routes.MapRoute(
    "DrillDown", // Route name
    "Location/{State}/{City}/{ZipId}", 
    new { controller = "Location",
          action = "DrillDownAction", 
          State= UrlParameter.Optional, 
          City = UrlParameter.Optional,
          ZipId = UrlParameter.Optional
    }
    

    Which would give you urls in the form: http://www.domain.com/Location/{State}/{City}/{ZipId}