0

I am using asp.net core 3.1 and receiving values from URL. Its working fine but when I add "+" sign as a part of URL, it gives 404. Example : localhost:9000/api/get/%2B12/values

Faisal
  • 45
  • 1
  • 7

2 Answers2

1

+ is a special character. It should ideally be should be URL encoded as %2B.
Turns out it's not really required though (like in the screenshot below) but I still recommend it. (See: URL encoding the space character: + or %20?)

Here's a working example controller:

[ApiController]
public class ExpController : Controller
{
    [Route("/api/exp/{arg}/values")]
    public IActionResult Test(int arg) =>
        Ok($"The arg is: {arg}");
}

enter image description here

Note how the route parameter is a int. For more complex values (other than just 12, +12, -12; eg: 12+12) the route will need to be a string instead.

galdin
  • 10,765
  • 7
  • 54
  • 69
1

version above IIS7 will refuse to request the URL contains symbols such as '+' by default. The following modifications are required. You need add the following nodes in web.config:

<system.webServer>
    <security>
        <requestFiltering allowDoubleEscaping="true"/>
    </security>
</system.webServer>

But now the .net core project does not have web.config to configure IIS options. You need to go to the location:

vs project location /.vs/config/applicationhost.config to add the above node.

Note that the .vs folder is a hidden folder and needs to be set visible.

mj1313
  • 7,235
  • 1
  • 7
  • 29