Web API - Reserved Word?

Web API - Reserved Word?

本文关键字:Word Reserved API Web      更新时间:2023-09-26

**有一个非常简单的web API:

WebApiConfig.cs:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional});

我可以通过我的数据服务控制器调用此web api调用:

[HttpGet]
public HttpResponseMessage GetUsers(string id)
{
    var cl = _SecurityRepository.GetUsers(id);
    return Request.CreateResponse(HttpStatusCode.OK, cl);
}

逻辑很简单:在数据库中搜索以id 中最后三个字母开头的任何姓氏

呼叫:

http://example.com/api/dataservice/Getusers/lop

结果:

[
  {
    "userID": "DLopez",
    "firstName": "Don",
    "lastName": "Lopez",
    "department": "Information Technology"
  },
  {
    "userID": "SLOPER",
    "firstName": "Steve",
    "lastName": "Loper",
    "department": "First Services"
  },
  {
    "userID": "DLOPES",
    "firstName": "Davey",
    "lastName": "Lopes",
    "department": "Public Info"
  }
]

一切都很好。是 啊然而,在一种情况下,我得到了一个资源找不到错误与这个调用:

http://example.com/api/dataservice/Getusers/con

我可以使用id=can、cone、com等,。。。这些api调用都没有问题。他们都工作得很好。我一在con上搜索就找不到资源!有人知道线索吗?我迷路了。

是的,"con"是一个保留字,不允许使用。解决方法是在Web.config的<system.web>下包含以下内容。
<httpRuntime relaxedUrlToFileSystemMapping="true" />

取自这个答案。