Web API - Reserved Word?

时间:2015-10-30 22:13:25

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

**Have a very simple web API:

WebApiConfig.cs:

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

I can make calls to this web api calling via my data service controller:

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

Logic is very simple: Searches the DB for any last names that start with the last three letter in id

The call:

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

The Results:

[
  {
    "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"
  }
]

It all works great. Yeah! However, in one circumstance I get a resource cannot be found error with this call:

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

I can use id=can, cone, com, etc,... There are no problems with any of those api calls. They all work great. As soon as I search on con -> resource cannot be found! Does anyone have a clue? I'm lost.

1 个答案:

答案 0 :(得分:2)

Yep, "con" is a reserved word and not allowed. The workaround is to include the following under <system.web> in your Web.config.

<httpRuntime relaxedUrlToFileSystemMapping="true" />

Taken from this answer.