1

I'm having a challenge figuring out the best way to define my hierarchical based routes dynamically.

I'm looking to achieve database driven links similar to the below:

/Illinois/
/Illinois/Chicago/
/Illinois/Chicago/Id
/California/
/California/Los-Angeles/
/California/Los-Angeles/Id
/New-York/
/New-York/New-York-City/
/New-York/New-York-City/Id

And so on, I don't want to have to define a controller for each state, but I'm not 100% against if if it's "the right way".

Stephen
  • 47
  • 1
  • 5
  • In addition to those do you also expect to have routes like `{controller}/{action}` such as `/Home/Index` for example? Because then you will need to hit the database when resolving the routes in order to know that `Home` is not a state in the U.S. and `Index` is not a city. – Darin Dimitrov Mar 08 '16 at 07:00
  • Possible duplicate of [Multiple levels in MVC custom routing](http://stackoverflow.com/questions/31934144/multiple-levels-in-mvc-custom-routing) – NightOwl888 Mar 08 '16 at 07:09
  • The urls you have shown will not work by themselves assuming you have other controller in your app. You will need some identifying prefix such as `/Locations/Illinois`, `/Locations/California/Los-Angeles/` etc and then you need a `LocationsController` with just one method to handle all the above –  Mar 08 '16 at 07:09
  • @DarinDimitrov - good questions, yes, I still need to be able to use traditional routes – Stephen Mar 08 '16 at 21:49
  • @StephenMuecke - The business requirements do allow me to add a leading controller name, such as /Locations/ or similar – Stephen Mar 08 '16 at 21:50
  • It does not need to match the controller name - it could be `/x/Illinois` - but you need something to identify the route, otherwise you will need to create a custom route for every state (you can still have just one method and one controller) –  Mar 08 '16 at 21:52

1 Answers1

2

You can create a controller like HomeController and use route attributes on the top of this controller and related action to hide the route url and call your locations and ids in route like this:

[RoutePrefix("")]
public class HomeController : Controller
{
    Route("{state?}/{city?}/{id?}")
    public ActionResult Index(string state, string city, int id)
    {
        //your codes
        return View();
    }
}
Mostafa Azarirad
  • 629
  • 1
  • 6
  • 27
  • check true for answare – Mostafa Azarirad Mar 09 '16 at 05:35
  • Looks like I may have jumped the gun on this one. I'm having problems getting traditional routes to work using the above. Is there a way for me to have a list of acceptable states? I would prefer to not have to list all of them out, but I can for the state and city calls. Or, is there a way for me to exclude something? In this case, I'm posting to "/Lead", but it's looking for lead in the database. Otherwise I get an error that "Multiple controller types were found that match the URL" – Stephen Apr 03 '16 at 22:39
  • I ultimately did end up using this route, and it works great. The fix for traditional routes was to use regex expressions to ignore them. Not the cleanest of solutions, but there aren't too many, so it still works pretty well. – Stephen Oct 27 '16 at 08:37