Search code examples
asp.net-coreroutesurl-routing

Handling '+' in ASP Net Core routes


How are '+' handled in ASP Net Core routes? I have a route defined like this:

"/Test/{words}/{type:regex(^(AA|BB|CC)$)}/{search2}"

with parameters of the function being string[] words, string type, string search2

With different URLS:

  • /Test/word1+word2/AA/blablabla => 404
  • /Test/word1%20word2/AA/blablabla => Ok, words={"word1 word2"}
  • /Test/word1,word2/AA/blablabla => Ok, words={"word1","word2"}

I don't understand the 404. any idea why it happens? I would have translated the '+' with a space.


Solution

  • You must encode a space; it is correct to encode a space as +, but only in the query string; in the path you must use %20. For + you can use %2b. (Http 1.1)