WebApi の 1 つを呼び出すことになっている Windows ストア アプリケーションで次のコードを実行しています。
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 で動作させています。HttpGet WebApi を呼び出すために Windows ストア アプリで動作しているものは次のとおりです ...
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
};
}
追加情報がお役に立てば幸いです。