1

I used ASP.NET MVC5, I have a URL like this:

localhost:/product/12/nexus5x/quartz

product :controller; 12 :id; nexus5x/quartz:name

But it's returning a 404 error because the last param in the url contains / and I don't have routing to support that. How can I do this? Sometimes the last param (productName) contains /.

The first solution is replace / with - but for some product I couldn't replace that.

0

2 Answers 2

1

You need to encode the URL. URL encoding involves converting the characters so that they can be included in the query string. The character / is encoded to %2F.

So the URL will become /product/12/nexus5x%2Fquartz

To encode the value in JavaScript you can call the encodeURI method.

var productName = "nexus5x/quartz";
var encodedProductName = encodeURI(productName);

To encode the value in C# you can call the Uri.EscapeUriString method.

Sign up to request clarification or add additional context in comments.

1 Comment

@osmanRahimi In C# you can use Uri.EscapeUriString msdn.microsoft.com/en-us/library/…
1

I believe that you should be able to use a catch all route. Just prefix an asterisk '*' to the last token.

Here is an example using Route attribute:

[Route("product/{id}/{*name}")]

The parameter 'name' here should catch everything past 'id', even if that includes a slash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.