views:

307

answers:

3

I will be launching a new site soon. One section of the site contains products.

I am currently trying to decide between different URLS for the ASP.NET routing :

**Products: **

 /products/1001
 /products/sku/1001
 /products/product/1001
 /products/view/1001

**Categories: **

 /products/category/cats
 /products/category/clothing

 /products/cats         - this is pretty nasty
 /products/clothing

I am primarily trying to decide which of the following I should adopt. I am currently doing the first.

  routes.MapRoute(
    "product-route-short",
    "products/{sku}",
    new { controller = "Products", action = "ViewProduct", sku = "1001"}
  );

or

  routes.MapRoute(
    "product-route-short",
    "products/sku/{sku}",
    new { controller = "Products", action = "ViewProduct", sku = "1001"}
  );

or one of the following

 * call the action 'sku' , 'details' , 'item'
 * use an [ActionName] attribute and leave the action method name as 'ViewProduct'

I really like the simplicity of /products/1001 so the user can see it and change it - but there is quickly a danger of it conflicting with other actions if I'm not careful. I also have to be sure to use 'product-route-short' when creating my URLS which is a very minor pain.

What does anyone think? What have you found a best practice to be? This example is pretty simple, but I want to be consistent across the site of course and don't want to have to retrofit everything once Google has already indexed everything for me.

If there are any especially good podcasts or articles on the subject (general or specific to ASP.NET) I'd appreciate links to them. I found a good podcast where one thing they specifically said was that 'this podcast is NOT about how to create good REST links'

+1  A: 

Note that SO itself lists this question on stackoverflow.com/questions/625213 (the / afterwards is ignored). So you at least have precedent :).

Simon Buchan
i do like the fact that whatever is after the final / is ignored - which makes it easy to copy and past URLs and still know what they refer to. also you can change the question title and existing links don't break
Simon_Weaver
+1  A: 

I think the shorter URLs you are making are fine. The beauty of the asp.net mvc routing engine is that, assuming you have made your links correctly in the views, you can make changes to the routes as you develop. So feel free to play around as you go.

Another thing to keep in mind is the constraints argument in the MapRoute method. You can always try something like this:

routes.MapRoute( 
"product-route-short", 
"products/{sku}", 
new { controller = "Products", action = "ViewProduct", sku = "1001" } , 
new { sku = @"[0-9]" } 
);

routes.MapRoute( 
"product-route-other", 
"products/{action}/id", 
new { controller = "Products", action = "ViewProduct" } 
);

That filter will make it so only skus between 0-9 will work. Obviously you would want to use a regex to fit your particular scenario, but that would help you avoid conflicts with actions. In this situation, if the value for sku was not between 0-9 then the routing engine would go to the next route and try to match that. It will do this until it finds a match.

With the combination of routes and constraints you should be able to have very clean url that make sense to the user.

Nathan Totten
+2  A: 

I would gear my URLs towards these goals...

1) Usability. It's really nice if you can tell what kind of page you'll get if you look at the URL. If it's short enough to type and/or guess, all the better.

2) Search Engines. You really want to get your keywords into those URLs. I know you want to use the ID to look up the product, but human beings and search engines want to see the product name there. Also, the closer a key word is to the start of the URL, the more importance is placed on it.

This is really just a skimming. If you want the best URLs you'll need to ensure the categories and product names are unique so you can look them up rather than using IDs in the address.

My suggested convention would be...

/products/

/products/clothing/

/products/clothing/blue-suede-shoes/

Sohnee