Reputation: 2202
I have a controller called "UploadsController". I have a GET action like so:
public string GetUpload([FromUri]string action)
{
return "hey " + action;
}
If I navigate to the following API URL in my browser, I get a successful response.
http://localhost:52841/MySite/api/uploads?action=testaction
However, when I try calling the API from code-behind in my WebForms app, I get a 404 response.
Here's what I have in my Global.aspx file (even though I believe the first should do it):
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
RouteTable.Routes.MapHttpRoute(
name: "Default2Api",
routeTemplate: "api/{controller}/{action}",
defaults: new { controller = "Uploads", action = "GetUpload" });
Here's how I'm calling the API:
// Send a request asynchronously continue when complete
client.GetAsync("http://localhost:52841/MySite/api/uploads?action=testaction").ContinueWith(
(requestTask) =>
{
// Get HTTP response from completed task.
HttpResponseMessage response = requestTask.Result;
// Check that response was successful or throw exception
response.EnsureSuccessStatusCode();
// Read response asynchronously as JsonValue
response.Content.ReadAsAsync<string>().ContinueWith(
(readTask) =>
{
var result = readTask.Result;
//Do something with the result
});
});
I thought I've done this before (with the RC version, using RTM now), but I can't seem to get this one.
As a side note, the request isn't showing in fiddler for some reason, which is kind of annoying when you're trying to debug these kind of stuff.
Any help is appreciated.
Upvotes: 3
Views: 1074
Reputation: 1229
Code sample:
RouteTable.Routes.MapHttpRoute(
name: "Default2Api",
routeTemplate: "api/{controller}/{action}",
defaults: new { controller = "Uploads", action = "GetUpload", custAction = RouteParameter.Optional});
Upvotes: 2
Reputation: 91
Yes I have been through the same problem most likely your issue is that webapi doesnt allow cross domain calls by default or at least this is what I know about it.
You need to add a CORS support to your web api code, follow the link this guy has shown how to add CORS to your webapi
http://code.msdn.microsoft.com/CORS-support-in-ASPNET-Web-01e9980a
Good Luck.
Upvotes: 1