views:

47

answers:

1

Hi all,

it is quite clear what the advantages of using nice urls in you web site are.

Let me clarify what i am asking by using an example. To note that the entities here cannot be uniquelly identified by name.

Say the user wants to see all the radio stations registered at your site that are available at a specific location. This is done by accessing the url: http://example.com/radios/1234/radio-paris-eiffel-tower

  • 1234 - in this case is the unique id of a location
  • radio-paris-eiffel-tower - is just additional info to make the url nicer.

If the users wants to see all locations where a specific radio station is accessibile they can use: http://example.com/locations/abcd/radio-xyz

  • abcd - here identifies the radio

Now the question: what would be a nice url for accesing the radio station details of a specific radio station at a specific location (like name, frequency, ...)?

http://example.com/radio/1234/radio-paris-eiffel-tower/abcd/radio-xyz or http://example.com/radio/details?radioid=1234&locationid=abcd/radio-xyz-at-paris-eiffel-tower

Do you have any suggestions and could you please also give me an example on how to code the route for your suggested url?

Thank you very much.

ps: actually thinking about the urls above, instead of http://example.com/radios/1234/radio-paris-eiffel-tower

nicer it would be http://example.com/1234/radio-paris-eiffel-tower/radios/

wdyt?

how could i map such a route to RadiosController.ListByLocation(int locationId)?

+2  A: 

Here's a suggestion using a custom route. I've set this up in the Global.asax.cs file:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Radio and Frequency Route", // Route name
        "radio/details/{radioId}/{locationId}/{someUnusedName}", // URL with parameters
        new
        {
            controller = "Some",
            action = "SomeAction",
            radioId = string.Empty,
            locationId = string.Empty
        }
    );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

}

The controller is pretty simple:

public class SomeController : Controller
{
    public ActionResult SomeAction(string radioId, string locationId)
    {
        ViewData["radioId"] = radioId;
        ViewData["locationId"] = locationId;

        return View();
    }

}

and it responds to URLs that are prefixed with /radio/details:

http://localhost:51149/radio/details/123/456/moo
http://localhost:51149/radio/details/abc/xyz/foo

Now you can build up any kind of friendly URL that suits your needs. Perhaps you want to change the /radio/details prefix to something else?

AndrewDotHay