使用默认的 web api 路由
With the default web api route
config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new
{
id = RouteParameter.Optional
}
);
还有一个控制器
public class TestController : ApiController
{
[HttpGet]
public HttpResponseMessage Get(string id)
{
return Request.CreateResponse(HttpStatusCode.OK, id);
}
}
对'api/test/1'
返回 1
如果由于某种原因您向 'api/test/1%20' 发送请求
If for some reason you send a request to 'api/test/1%20'
404 路线.
现在这个例子可能看起来很傻,因为浏览器会修剪尾随空格,但是
Now this example may seem silly since browsers trim trailing spaces, but
对于像 'api/{controller}/{id}/{extrastuff}'这样的路由
'1 ' 中的空格将转换为 '1%20' 并且请求将在找不到路由上出现 404.
the space in '1 ' would convert to '1%20' and the request will 404 on the route not being found.
您的问题与 WebAPI 本身无关,而是 Asp.Net 如何处理一些特定的 url.而 Asp.Net 以非常偏执的方式处理这些 url,因此您需要告诉它 放松.
Your issue has nothing to do with WebAPI itself but how Asp.Net handles some specific urls. And Asp.Net handles these urls in a very paranoid way, so you need to tell it to relax.
在 system.web 下将此行添加到您的 web.config:
Add this line to your web.config under system.web:
<httpRuntime relaxedUrlToFileSystemMapping="true" />
您可以阅读有关此主题的更多信息:
You can read more about this topic:
同样在 SO:
这篇关于当 URL 中有尾随空格时,WebAPI 路由 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
ASP.NET Core 使用 Azure Active Directory 进行身份验证并ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 进行身
ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用)
ASP Core Azure Active Directory 登录使用角色ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登录使用角色)
如何获取守护进程或服务器到 C# ASP.NET Web API 的How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何获取守护进程或服务器到 C# ASP.N
.Net Core 2.0 - 获取 AAD 访问令牌以与 Microsoft Graph.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 获取 AAD 访问令牌以与 Microsoft Graph 一起使用)
异步调用时 Azure KeyVault Active Directory AcquireTokenAAzure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(异步调用时 Azure KeyVault Active Directory AcquireTokenAsync 超