With the default web api route
config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new
{
id = RouteParameter.Optional
}
);
and a controller
public class TestController : ApiController
{
[HttpGet]
public HttpResponseMessage Get(string id)
{
return Request.CreateResponse(HttpStatusCode.OK, id);
}
}
A request to 'api/test/1'
returns 1
If for some reason you send a request to 'api/test/1%20'
the route 404's.
Now this example may seem silly since browsers trim trailing spaces, but
for a route like 'api/{controller}/{id}/{extrastuff}'
the space in '1 '
would convert to '1%20'
and the request will 404 on the route not being found.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…