繁体   English   中英

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

[英]Trouble calling a ASP.NET MVC 4 WebApi from a Windows Store Application

我在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();
}

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 };
}

使用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

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

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

我甚至尝试在本地使用附加到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 }
    );
}

因此,我认为/ 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;
        }
    }
}

......它正在调用以下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
        };
}

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

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

我将签名更改为

public HttpResponseMessage Post(FormDataCollection data)

它起作用了。

你也可以这样做,

public HttpResponseMessage Post(VinEntity vinEntity)

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

Rick Strahl在此问题上有一篇文章http://www.west-wind.com/weblog/posts/2012/Sep/11/Passing-multiple-simple-POST-Values-to-ASPNET-Web-API

您是否在清单中打开了Internet网络?

它可能是跨域Ajax安全问题。 请在此处查看JSONP信息。 => http://json-p.org/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM