gpt4 book ai didi

c# - 从 Windows 应用商店应用程序调用 ASP.NET MVC 4 WebApi 时遇到问题

转载 作者:太空狗 更新时间:2023-10-29 23:40:16 24 4
gpt4 key购买 nike

我在 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)

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

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

关于c# - 从 Windows 应用商店应用程序调用 ASP.NET MVC 4 WebApi 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12791223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com