'How to remove trailing slash in Web API routing?
I have a URL which has some id which can have multiple "." values in it(Like 1.1, 1.1.0 , 2.0.0.1) like this and I want to treat it as string only not decimal.
So these work perfectly fine when I put a trailing slash and I get a string value in Route Value Dictionary https://root/myApp/Download/File/1.1/
https://root/myApp/Download/File/1.1.0/
But without slash : https://root/myApp/Download/File/1.1
It redirects to https://root Url
Route Config
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I figured that the id parameter above, when I pass a normal integer it works. Eg: https://root/myApp/Download/File/1
But when I pass a decimal value like "1.1" or "1.1.0" and Without trailing Slash it does not work and redirects to root path. Can anyone explain why this may be happening and how to work without the trailing slash ?
Solution 1:[1]
Send your Controller code, i need view your Action.
In my API i'm using Route decorator with string parameter to resolve similar problem.
Ex.:
[ApiController]
public class HomeController : ControllerBase
{
[HttpGet]
[Route("download/file/{id}")]
public IActionResult Get([FromQuery]string id)
{
string[] stringArray = id.Split('.')
...
}
}
I need more informations for correct help you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Diego Lobo |
