3

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.

7
  • 1
    Works on my machine. :-) First step is to get Fiddler working here. Take a look at Rick's post on configuring Fiddler to work with HttpClient requests. Also, as you note, you don't need that second route in your global.asax file. The first one is good enough. Commented Nov 19, 2012 at 18:43
  • Tried the web.config method, fiddler still not capturing. Commented Nov 19, 2012 at 21:13
  • Ah, now I believe you're hitting the classing Fiddler localhost issue. Per the Fiddler docs "Important: Regardless of other settings, .NET will always bypass the Fiddler proxy for URLs containing localhost. So, rather than using localhost, change your code to refer to the machine name." Commented Nov 21, 2012 at 6:03
  • My page doesn't load/connect when I use the machine name instead of localhost. Commented Nov 27, 2012 at 16:16
  • Just to confirm this code also works OK on a quick test harness I put together too. The few other SO questions along these lines usually end up being an rouge trailing "/" at the end of the URL? Is the url in your question the exact URL you are trying to call? Commented Nov 28, 2012 at 9:04

2 Answers 2

2
+25
  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});
Sign up to request clarification or add additional context in comments.

3 Comments

Didn't work. Note, the "custAction" would be passed as a query string parameter - it shouldn't have to be part of the route definition (?) Also, even without the paramter, I get a 404.
Did you try defining the route definition before the default one?
I tried swapping them - yes. But again - even without the parameter (and an action with out a param) it gives a 404.
1

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.

4 Comments

Are you calling your webapi from the same solution? Like are you writing the consumer code in the same solution as the webapi? or webapi is deployed somewhere else and you are using a separate application to consume the webapi if yes, then try its the CORS issue for sure. I had the same issue.. Is your browser showing an error like origin access etc?
No - in my example I have the web api and client code in the same solution.
localhost:52841/MySite/api/uploads/GetUpload?action=testaction try this url then.. you are providing the wrong route then
I mean try removing this defaults: new { controller = "Uploads", action = "GetUpload" } from your second route and use this defaults: new { id = System.Web.Http.RouteParameter.Optional } instead and then try calling this url. localhost:52841/MySite/api/uploads/GetUpload?action=testaction I hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.