ASP.NET MVC route by a city then controller and action

51 Views Asked by At

I have a basic ASP.NET MVC5 app with a Restaurant class as my model and I am currently using the default out of the box routing.

  • /Restaurant/Create

  • /Restaurant/Details

  • /Restaurant/Index

Previously this was only based on one city. Now I would like to make this multi city and route it in such a way

  • London/Restaurant/Index

  • Manchester/Restaurant/Index

  • Birmingham/Restaurant/Index

I've been reading up on custom routing in ASP.NET, attribute routing, routing constraints but I cannot figure out how to achieve the above.

I tried changing the default route in RouteConfig.cs to the following but this does not work.

    routes.MapRoute(
        name: "Default",
        url: "{city}/{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

Here is my Restaurant Controller Index signature

        public ActionResult Index(int? RestaurantId)
        {
            ViewBag.Restaurants = new SelectList(db.Restaurants.ToList().OrderBy(r => r.RestaurantName), "RestaurantID", "RestaurantName");

            return View(db.Restaurants.ToList());
        }
1

There are 1 best solutions below

0
Daark_Archer On

In the interest of anyone reading this post in the future:I found a different way to achieve my goal of using cities. I read up on subdomains and found that I could use:

  • london.mydomain.com
  • manchester.mydomain.com
  • birmingham.mydomain.com

This is by using a custom RouteBase class as described in this interesting article.