Reputation: 1339
I have to use the 4.0 framework to create a web API but I am not able to make a request:
public class ChartDataApiController : ApiController
{
[HttpGet]
public List<ChartDatum> GetData(int lowerBound, int upperBound)
{
return new List<ChartDatum>();
}
}
I have this at the moment just to confirm that I can actually hit this action. The request:
var url = window.location.protocol + "//" + window.location.host + "/api/ChartDataApi/GetData";
$.ajax({
url: url,
type: "GET",
data: {
lowerBound: lowerBound,
upperBound: upperBound
},
dataType: "json",
success: function(response) {
graph.draw(response);
},
error: function(message) {
//error handler logic
}
});
I just cannot hit this action. I have tried converting it to a POST just to see if I could get it to work but to no avail. Server error gives a 404 (had to modify in order to submit):
GET http:// (url) : port# /api/ChartDataApi/GetData?lowerBound=0&upperBound=29 404 (Not Found)
I can make a request just fine so long as I don't try to pass any parameters either via the query string or through the request body. I have also tried prefixing the parameters with the [FromUri]
attribute but this also didn't work.
Here is the web api route config (simply the default):
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
I have made these sorts of requests before, but always in the 4.5 framework and think it may be a difference between frameworks.
Upvotes: 1
Views: 1064
Reputation: 5899
You are requesting the wrong resource. In your case, you should issue a GET
request to the /api/ChartDataApi
endpoint.
HttpGetAttribute
applied to the GetData
method simply indicates that this method is responsible for hangling HTTP GET
requests; there is no need to explicitly specify GetData
method name in the request URI.
Upvotes: 2