1

I have set up the following route in my MVC project:

            routes.MapRoute(
                "Product",
                "product-{pathname}-{productId}",
                new { controller = "Product", action = "ProductPage" });

This works fine for urls like product-test-title-1234 but if the pathname bit of the url contains product-, a 404 gets thrown - Is there a way to allow for a url like product-test-product-title-1234?

Weirdly, if I put the second product just before the product id (eg product-test-title-product-12345), then the route works and the page is displayed, anywhere else and it throws a 404

Update

I think the problem may lie with the fact that 2 parts of the url match the route so I guess my question is how do you ensure that the route says that first product must be at the start of the url (and count any further instances of product as part of the pathname).

After further testing, I have found that instead of going to the product controller, this url goes to my catchall route: {*pathname} - not sure why this happens

Pete
  • 57,112
  • 28
  • 117
  • 166
  • Why a `-` (hyphen)? - instead of `product/{pathname}/{productId}` to generate `../product/test/product-title-1234` –  Sep 22 '17 at 08:32
  • @StephenMuecke unfortunately it is the path of the current website and we need to keep it the same so that we don't have to implement 301 permanents for all the old urls (otherwise when the new site replaces the old we will be punished on seo). Also it's what the marketing department want - I would have gone for the usual `/` structure too but apparently a shallow url structure is also better for seo rankings – Pete Sep 22 '17 at 09:35

1 Answers1

0

Is there a way to allow for a url like product-test-product-title-1234?

Yes, but the route URL you have defined includes 3 dashes and the URL you are matching contains 4. You can match that URL using something like:

        routes.MapRoute(
            "Product",
            "product-{pathname}-product-{productId}",
            new { controller = "Product", action = "ProductPage" });

but that probably isn't what you are looking for. The problem here is that when you use - as the delimiter between your route placeholders, you the placeholder itself cannot contain - or the Route class will get confused.

But routing is very flexible if you drop down to a lower level and inherit RouteBase, which is probably more along the lines of what you are looking for. You can then put whatever custom logic/URLs you want into the route, which allows you to exactly match an old URL scheme even if it was not very well organized.

See: Multiple levels in MVC custom routing

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Hi Nightowl, thanks for the answer, unfortunately this wouldn't work as it doesn't really solve anything - the path that has product next to the product id already works (see last sentence of question above) and I do not want to include a second pathname (ie I don't want a route like `product-{pathname}-product-{pathname1}-{productId}`) as I need the whole of the pathname bit to get the page data from a headless cms system – Pete Sep 25 '17 at 08:00
  • See the link I provided if you want it to go to a CMS system. Using `MapRoute` doesn't work for every scenario, but if you inherit `RouteBase` you can literally program your routes to work any way you like. – NightOwl888 Sep 25 '17 at 14:41