1

I wanna make my urls SEO friendly. I have seen a lot of articles and other posts on this subject, but all of them ends up with urls like: /Products/34/my-handbag

I wanna end up with urls like: /gucci/my-handbag

I already have the controls to actually get the product from the names "gucci" and "my-handbag", i just need my routing to send me to Products controller.

This is what I am working with right now:

routes.MapRoute(
    name: "ProductDetails",
    url: "{brand}/{title}",
    defaults: new { controller = "ProductViews", action = "Details", brand = "", title = "" }

Any suggestions?

AlexB
  • 7,302
  • 12
  • 56
  • 74
Axel Andersen
  • 330
  • 4
  • 14
  • See [this answer](http://stackoverflow.com/questions/31934144/multiple-levels-in-mvc-custom-routing/31958586#31958586) for the most flexible approach. – NightOwl888 May 25 '16 at 09:04

2 Answers2

3

You're on the right path. You can generate an url like: www.domain.com/gucci/some-gucci-product-name by means of the following route.

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

You can handle the title, search for the product in db if it's unique. If you have to add some unique number at the end of the url like; www.domain.com/gucci/some-gucci-product-name-13456

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

Hope it helps.

ali
  • 1,301
  • 10
  • 12
  • Hi, thank you for your suggestion. It just not working, I still get "The resource cannot be found." – Axel Andersen May 25 '16 at 08:08
  • Could you send the signature of the controller action that is supposed to handle the route and the route definition you used? – ali May 25 '16 at 08:20
  • routes.MapRoute( "Default", // Route name "{Brand}/{Details}", // URL with parameters new { controller = "ProductViews", action = "Details", Brand = UrlParameter.Optional, Details = UrlParameter.Optional }// Parameter defaults ); – Axel Andersen May 25 '16 at 08:45
  • public class ProductViewsController : Controller public ActionResult Details(string Brand, string Details) – Axel Andersen May 25 '16 at 08:46
  • @user3305839 Both route and action are correct, I also tried it on my computer, it's working. Your problem might be the order of routes if other route definition exists. If this is not the problem, your issue is definitely not related to the definition of the route. I think there are some threads for routing problems in stackoverflow. – ali May 25 '16 at 08:56
  • It worked when I changed the order of routes, thanks a lot – Axel Andersen May 25 '16 at 09:02
0

@ali is right, but this structure is better-

/Products/34/my-handbag
/product/{id}/slug

You can remove the last word in stackoverflow and enter. The page redirects you to current and correct url. It is a better approach. My comments are:

  1. If your website grows up, you can have same slug with different IDs and this is not limit to the problems that may appear. I know that it is not good for seo but it will happen.

  2. In the backend you can have better performance to find product with ID and its important for high visited website.

  3. It will be easy for you to change slug. If you change slug, old link will redirect to new link and you will not have any broken link.

In the backend find product with ID, then check the database to see if slug is equal to the parameter received from url. If it isn't, you can redirect with 301 status to correct url, just like stackoverflow.

Pankaj Dubey
  • 796
  • 3
  • 8
  • 32