Rivka
Rivka

Reputation: 2202

ASP.NET Web API in WebForms 404

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

Answers (2)

dicemaster
dicemaster

Reputation: 1229

  1. Try naming your query string parameter to something else (Right now it is "action"). I think that's one reason it's causing problems. Since MVC is convention-based, that might be causing problems.
  2. Also in your route declaration, try adding the query string parameter (Let me call it custAction).
  3. And declare custom route before default route.

Code sample:

RouteTable.Routes.MapHttpRoute(
name: "Default2Api",
       routeTemplate: "api/{controller}/{action}",
       defaults: new { controller = "Uploads", action = "GetUpload", custAction = RouteParameter.Optional});

Upvotes: 2

Aakif
Aakif

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

Related Questions