-1

I have a problem with my MVC. Think like a URL.

domain.com/product/any-product/1

domain.com/category/any-category/1

Can I change url like this?:

domain.com/any-product/1

domain.com/any-category/1

Also can I delete id?

context.MapRoute(
    name: "category",
    url: "category/{category_name}/{category_id}",
    defaults: new
    {
        controller = "Cateogory",
        action = "Index",
        Area = "Shop"
    }
);

context.MapRoute(
    name: "product_detail",
    url: "product/{category}/{product_name}/{product_id}",
    defaults: new
    {
        controller = "Product",
        action = "ProductDetail",
        Area = "Shop",
        product_name = UrlParameter.Optional,
        product_id = UrlParameter.Optional
    }
);
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Caner
  • 813
  • 1
  • 12
  • 26

1 Answers1

0

No, you can't do it exactly like that, but you can get closer. Think for a second about what you're saying you want:

/any-product/ => ProductController
/any-category/ => CategoryController

Now, logically speaking, how is MVC supposed to sort out which URL is referring to a product and which is referring to a category?

You could do something like:

/any-category/ => CategoryController
/any-category/any-product/ = > ProductController

Now, there's some way to for MVC to work out that the URLs are different. If there's something after the category part of the path, then it's a product.

As for removing the id portion, that depends. The point of the id in the first place is that you need some sort of unique identifier for a record that should be loaded. If "any-product" is a string that can uniquely identify the record (you have column in the database with that value and there's only one with that value), then you can use that string to look up the entity instead. Then, no, you no longer need the id portion of the URL. However, if the string is not unique or doesn't correspond to data in the database, then you need the id to locate the record, and it cannot be removed.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Actually you can. But you have to process things through an intercept controller. `/any-product/ => InterceptController => ProductController /any-category/ => InterceptController => CategoryController` The intercept controller would analyze the values from the URL and determine whether the directive was a product or a category and then could do a RedirectToRoute or RedirectToAction call to ultimately process the request. Obviously you could not have a product and a a category named the same. However this tends to muddle things quite a but SEO wise. – Wolfie Oct 29 '15 at 20:43
  • Redirecting merely creates the same problem in reverse; you still have to have separate URL structures for each type of thing, but now you just have an intermediary step to get there. The OP could do as you suggest and then merely conditionally return different views. That would allow them to share the same URL structure, but then you end up with a very heavy and complex action method. Additionally, there may be instances where you simply can't differentiate. What if a category and a product have the same slug? You're borked. – Chris Pratt Oct 30 '15 at 12:56
  • Yes, I agree. Its not a wise circumstance to have a unified url like this. And thats also why I said you couldn't have a product and a category named the same. For our CMS, I use /cat/somecatname/othercatname, etc structure and /p/someproduct for products. That way you can route out /cat and /p and send them to appropriate controllers. Just because you can do something doesn't mean you should do it. I merely offered the option for consideration in case it fit a scenario. – Wolfie Oct 30 '15 at 15:00