memical
memical

Reputation: 2503

Nice URLs based on multiple parameters

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

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

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)?

Upvotes: 3

Views: 1372

Answers (1)

a7drew
a7drew

Reputation: 7811

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?

Upvotes: 3

Related Questions