I've got the following code running in a Windows Store application that is supposed to call one of my WebApis:

using (var client = new HttpClient())
{
    var parms = new Dictionary<string, string>
        {
            {"vinNumber", vinNumber}, 
            {"pictureType", pictureType}, 
            {"blobUri", blobUri}
        };

    HttpResponseMessage response;

    using (HttpContent content = new FormUrlEncodedContent(parms))
    {
        const string url = "http://URL/api/vinbloburis";

        response = await client.PostAsync(url, content);
    }

    return response.StatusCode.ToString();
}

The WebApi code looks like this:

[HttpPost]
public HttpResponseMessage Post(string vinNumber, string pictureType, string blobUri)
{
    var vinEntity = new VinEntity
        {
            PartitionKey = vinNumber,
            RowKey = pictureType, 
            BlobUri = blobUri
        };

    _vinRepository.InsertOrUpdate(vinEntity);

    return new HttpResponseMessage { Content = new StringContent("Success"), StatusCode = HttpStatusCode.OK };
}

Using Fiddler, I've observed the following ... here's what the request looks like:

POST http://URL/api/vinbloburis HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: URL
Content-Length: 113
Expect: 100-continue

vinNumber=vinNumber&pictureType=top&blobUri=https%3A%2F%2Fmystorage.blob.core.windows.net%2Fimages%2Fimage.png

But the response is an error with little/no information:

{"Message":"An error has occurred."}

I've even tried locally with the debugger attached to the WebApis and my Post method never catches.

Does anyone see something I've missed here? I feel like I'm looking right at it but not seeing it. I should add that I am able to call an HttpGet method while passing a parameter through the querystring. Right now the problem is only with this HttpPost.

Thanks!

UPDATE: Based on some good comments from folks I'm adding some more details.

I have the default routes configured for WebApis ...

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

Consequently, I think /URL/api/vinbloburis should work. Additionally, as alluded above, I currently have this working with an HttpGet. Here's what's working in the Windows Store app to call the HttpGet WebApi ...

using (var client = new HttpClient())
{
    using (var response = await client.GetAsync(uri))
    {
        if (response.IsSuccessStatusCode)
        {
            var sasUrl = await response.Content.ReadAsStringAsync();
            sasUrl = sasUrl.Trim('"');

            return sasUrl;
        }
    }
}

... and it's calling the following WebApi ...

[HttpGet]
public HttpResponseMessage Get(string blobName)
{
    const string containerName = "images";
    const string containerPolicyName = "policyname";

    _helper.SetContainerPolicy(containerName, containerPolicyName);

    string sas = _helper.GenerateSharedAccessSignature(blobName, containerName, containerPolicyName);

    return new HttpResponseMessage
        {
            Content = new StringContent(sas),
            StatusCode = HttpStatusCode.OK
        };
}

I hope the additional information helps!

share|improve this question
Just to confirm - have you changed the name of the method you are calling? Currently your code sample has a method name of POST but your HTTP request is trying to call vinbloburis ? This would cause the error you see. – LewisBenge Oct 9 at 0:44
If you do the POST from fiddler, does it work? – Darrel Miller Oct 9 at 0:58
Have you tried using PostJsonAsync? Can you please share your routing config? – indyfromoz Oct 9 at 1:00
These are great questions, folks! @LewisBenge, I don't think that's the issue - I've shared how the WebApis are registered and some additional info related to the HttpGet. – Wade Oct 9 at 1:13
feedback

3 Answers

up vote 1 down vote accepted

I copied your code, as is and I was getting a 404 error.

I changed the signature to

public HttpResponseMessage Post(FormDataCollection data)

and it worked.

You can also just do this,

public HttpResponseMessage Post(VinEntity vinEntity)

and the model binder will do the mapping work for you.

Rick Strahl has a post on the issue here http://www.west-wind.com/weblog/posts/2012/Sep/11/Passing-multiple-simple-POST-Values-to-ASPNET-Web-API

share|improve this answer
I suspect you're on to something here. However, I've tried to make both changes and still I get the same error response and my method isn't called. I'll keep digging into this ... – Wade Oct 9 at 1:40
@wade Plug this asp.net/web-api/overview/testing-and-debugging/… tracewriter into your config. IT will show you exactly what is causing the error. – Darrel Miller Oct 9 at 1:43
Okay, as is so often the case, the problem is an unrelated issue that caused the HTTP/1.1 500 Internal Server Error. I use Ninject and it turns out that one of my bindings was causing the constructor to throw an exception. Once I resolved your suggestion Post(VinEntity vinEntity) worked perfect and was a simple solution. I'll try to update my primary post with some more information once I resolve. TX! – Wade Oct 9 at 2:35
feedback

It may be a Cross Domain Ajax Security issue. See JSONP info here. => http://json-p.org/

share|improve this answer
He is making the call from a Windows Store app. There are no cross domain issues with Windows Clients. – Darrel Miller Oct 9 at 0:53
feedback

Have you turned on Internet Networking in your manifest?

share|improve this answer
Yes - both Internet (Client) and Internet (Client & Server) are turned on (although I think I only need Client). I should have added that I have another service I'm calling successfully but the difference is that it's an HttpGet and the parameter is passed int he querystring. Right now I'm just struggling to call the HttpPost. TX! – Wade Oct 9 at 0:39
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.