1

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());
        }
3
  • In what way does it not work? You get a 404? Could you add signature of Restaurant/Index controller action please, also if there are any routes above "Default" in your RouteConfig pls Commented Feb 21, 2024 at 10:08
  • @dove So sorry I forgot to reply! Yes I get a 404. Commented Mar 4, 2024 at 21:13
  • Added controller signature above now, there were no other routes apart from the default one. Commented Mar 4, 2024 at 21:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.