【问题标题】:Web API - Reserved Word?Web API - 保留字?
【发布时间】:2015-10-30 22:13:25
【问题描述】:

**有一个非常简单的 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 -> 资源就找不到了!有人有线索吗?我迷路了。

【问题讨论】:

  • 找到用户了吗?我们可以看到 GetUsers(...) 方法的代码吗?有错误处理吗?

标签: javascript c# json angularjs asp.net-web-api


【解决方案1】:

是的,“con”是保留字,不允许使用。解决方法是在您的 Web.config 中的 <system.web> 下包含以下内容。

<httpRuntime relaxedUrlToFileSystemMapping="true" />

取自this answer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多