ASP.NET Core : AddAreaPageRoute - parameter more than one time in the route

Viewed 104

What I am trying to do is something like this:

services
   .AddRazorPages()
   .AddRazorPagesOptions(options =>
   {
       options.Conventions.AddAreaPageRoute("Products", "/DownloadDoc", "Products/{keycode}/{keycode}-document.pdf");
   });

So if keycode was productTest, the url generated should be:

Products/productTest/productTest-document.pdf

Not sure if it is possible or not?

Currently it throws this error:

RoutePatternException: The route parameter name 'keycode' appears more than one time in the route template.

I have even tried things like this:

options.Conventions.AddAreaPageRoute("Products", "/DownloadDoc", "Products/{keycode2:keycode}/{keycode}-document.pdf");
2 Answers

actually, the problem is the naming problem. The URL format doesn't support multi able same parameters name.

Can you try like this

       options.Conventions.AddAreaPageRoute("Products", "/DownloadDoc", "Products/{keycode}/{keycode2}-document.pdf");

According to your route rule Products/{keycode}/{keycode}-document.pdf, this rule means it will match the two part for the url with the same parameter. This is not expected inside the asp.net core rule mapping rule.

If you think this two parameter should be the same, you could make a check by using a specific middleware or else.

If you make sure this two parameters will be the same, I think just one keycode is enough.

Related