Ste*_*eve 9 c# asp.net routing asp.net-mvc-routing asp.net-web-api
使用默认的Web api路由
config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new
{
id = RouteParameter.Optional
}
);
Run Code Online (Sandbox Code Playgroud)
和一个控制器
public class TestController : ApiController
{
[HttpGet]
public HttpResponseMessage Get(string id)
{
return Request.CreateResponse(HttpStatusCode.OK, id);
}
}
Run Code Online (Sandbox Code Playgroud)
请求 'api/test/1'
回报 1
如果由于某种原因你发送请求 'api/test/1%20'
路线404的.
现在这个例子看起来很愚蠢,因为浏览器会修剪尾随空格,但是
对于像这样的路线 'api/{controller}/{id}/{extrastuff}'
'1 '
将转换为空间,'1%20'
并且在未找到的路径上请求404.
nem*_*esv 33
您的问题与WebAPI本身无关,但Asp.Net如何处理某些特定网址.Asp.Net以非常偏执的方式处理这些网址,因此您需要告诉它放松.
将此行添加到您的web.config下system.web
:
<httpRuntime relaxedUrlToFileSystemMapping="true" />
Run Code Online (Sandbox Code Playgroud)
您可以阅读有关此主题的更多信息:
同样在SO:
归档时间: |
|
查看次数: |
6340 次 |
最近记录: |