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
2 Answers
+ 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}");
}
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.
- 10,765
- 7
- 54
- 69
-
localhost:9000/api/get/%2B12/values – Faisal Nov 10 '20 at 12:44
-
@Faisal I've added sample code, play around with it. – galdin Nov 10 '20 at 13:06
-
I have tried this but only + (%2B) is not working.%2F is working fine.Any idea? – Faisal Nov 10 '20 at 13:39
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.
- 7,235
- 1
- 7
- 29