无法从Windows应用商店应用程序调用ASP.NET MVC 4 WebApi

Wad*_*ade 5 c# asp.net-web-api windows-store-apps

我在Windows应用商店应用程序中运行以下代码,该应用程序应该调用我的一个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();
}
Run Code Online (Sandbox Code Playgroud)

WebApi代码如下所示:

[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 };
}
Run Code Online (Sandbox Code Playgroud)

使用Fiddler,我观察到以下内容......这是请求的样子:

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
Run Code Online (Sandbox Code Playgroud)

但响应是一个错误,很少/没有信息:

{"Message":"An error has occurred."}
Run Code Online (Sandbox Code Playgroud)

我甚至尝试在本地使用附加到WebApis的调试器,我的Post方法永远不会捕获.

有没有人看到我错过的东西?我觉得我正在看着它,但没有看到它.我应该补充一点,我可以在通过查询字符串传递参数时调用HttpGet方法.现在问题只出在这个HttpPost上.

谢谢!

更新:根据人们的一些好评,我正在添加更多细节.

我有为WebApis配置的默认路由...

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

因此,我认为/ URL/api/vinbloburis应该可行.另外,如上所述,我目前正在使用HttpGet.以下是在Windows应用商店应用中调用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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

......它正在调用以下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
        };
}
Run Code Online (Sandbox Code Playgroud)

我希望附加信息有所帮助!

Dar*_*ler 2

我按原样复制了您的代码,但收到 404 错误。

我将签名更改为

public HttpResponseMessage Post(FormDataCollection data)
Run Code Online (Sandbox Code Playgroud)

它起作用了。

你也可以这样做,

public HttpResponseMessage Post(VinEntity vinEntity)
Run Code Online (Sandbox Code Playgroud)

模型绑定器将为您完成映射工作。

Rick Strahl 在此处发表了有关此问题的帖子http://www.west-wind.com/weblog/posts/2012/Sep/11/Passing-multiple-simple-POST-Values-to-ASPNET-Web-API