Th3Fix3r
Th3Fix3r

Reputation:

ASP.NET MVC - Mapping more than one query string parameter to a pretty url

I am a bit stuck on the design of my seo friendly urls for mvc....Take for example the following url: http://myapp/venues/resturants.aspx?location=central&orderBy=top-rated

With my mvc app i have mapped it as follows: http://myapp/venues/list/resturants/central/top-rated
{controller}/{action}/{category}/{location}/{order}

Now the only problem is that location and order are optional...so it should be possible to submit a request like: http://myapp/venues/list/resturants/top-rated . This proves to be a problem when the request hits the controller action, the location parameter has picked up "top-rated", naturally.

Any suggestions? I' am considering using explicit querystrings to handle more than one parameter but this is really my last option as i dont want to sacrifice SEO too much.

Has anyone eles run into such dilemmas? And how did you handle it?

Thanks in advance!

Upvotes: 7

Views: 11695

Answers (5)

Th3Fix3r
Th3Fix3r

Reputation:

ok guys just posting a solution i've been playing with so far.

I have set up my routes using constraints as follows:

        routes.MapRoute(
            "VenuesList",                                              
            "venues/list/{category}/{location}/{orderBy}",             
            new { controller = "venues", action = "list", category = "", location = "", orderBy = "" },
            new { location = "central|east|west|south", orderBy = "top-rated|price" }
        );

    routes.MapRoute(
            "VenuesListByLocation",                                              
            "venues/list/{category}/{location}",                           
            new { controller = "venues", action = "list", category = "", location = "" },
            new { location = "central|east|west|south" }
        );

        routes.MapRoute(
            "VenuesListByOrder",                                              
            "venues/list/{category}/{orderBy}",                           
            new { controller = "venues", action = "list", category = "", orderBy = "" },
            new { orderBy = "top-rated|price" }
        );

        routes.MapRoute(
            "VenuesListDefault",                                              
            "venues/list/{category}",                           
            new { controller = "venues", action = "list", category = "" }

        );

        routes.MapRoute(
            "Default",                                              
            "{controller}/{action}/{id}",                           
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        ); 

The idea is that if the validation fails it will go to the next route in the list...eventually hitting the default.

Needs some more testing but has worked well so far...

Upvotes: 2

Sergio
Sergio

Reputation: 8269

Why don't you create a property in the page for each possible querystring parameter?

This way you can handle it any way you choose with just a few lines of code...

Upvotes: 0

Giraffe
Giraffe

Reputation: 2023

Assuming that the allowed values for location and order are unique (i.e. when they come in, you can tell them apart, or else if they only supply one, how are you going to know if it's a location or an order?), then you could just take two parameters and work out what they are in the controller.

Route: {controller}/{action}/{param1}/{param2}

Controller action:

public ActionResult MyAction(string param1, string param2)
{
    string location;
    string order;
    if (!ParseLocation(param1, out location))
    { ParseLocation(param2, out location); }
    // ...
}

Not particularly elegant, but does let you have the URLs you want.

Upvotes: 2

Todd Smith
Todd Smith

Reputation: 17282

Click on your profile link and look at the URLs for Stats, Recent, Response, etc.

Examples:

with no sort it defaults to stats

Optional paramters should be query parameters

Upvotes: 14

GalacticCowboy
GalacticCowboy

Reputation: 11769

You will always have this issue if you have multiple optional parameters. Either make one or both of them non-optional (and positioned earlier in the query string than the optional one) or use the querystring parameter notation.

Upvotes: 2

Related Questions