rcastagna
rcastagna

Reputation: 305

MVC 3 Routing Confusion

OK, I've been looking through a representative sample of the MVC 3 routing questions/answers, and I answered 80% of my question. A good thing, but now the most difficult part remains unsolved.

In my world, I have a URL localhost/Location/Guid

Works great, based off of this route that I've entered before my default route:

routes.MapRoute(
    "Location",
    "Location/{id}",
    new { controller = "Location", action = "Details", id = System.Guid.Empty }
);

I can pass in the Guid for the location I want displayed, but that's down-right FUGLY. I'd really like to see the URL displayed in the address bar, and linked within a page as: localhost/Location/Paris

Obviously, I could change the parameter to a string and pass in "Paris", but I don't understand how to get the database to understand that I want Paris, France and not Paris, Texas without the use of the Guid.

I really need some help on this one, not just to solve my immediate problem, but also to better understand routing in general. I thought I had a handle on it sufficient for my needs, but obviously, I don't.

Thanks in advance!

Upvotes: 0

Views: 95

Answers (1)

Giscard Biamby
Giscard Biamby

Reputation: 4619

You're going to have to put something in the URL to disambiguate between Paris, France, and Paris, TX.

  1. You could set up your route pattern to be `/Location/{country}/{region}/{city}', or you could slugify every possible location in your application
  2. Use those location slugs: /Location/{locationSlug}. If you have multiple locations named "Paris", you slugify them and append an auto-incrementing # to the end (e.g., /Location/paris, /Location/paris-1, /Location/paris-2). Or you can add region/city/country info to the slug to disambiguate, instead of the #.
  3. Do it stack overflow question style, where you have an ID, and then the location slug. It looks prettier and more concise if you are able to convert or map your GUID's to integers, so your pattern would be like this: Location/{id}/{locationSlug} (e.g., /Location/1245/paris).

Upvotes: 2

Related Questions