我有以下代码:
HttpClient client = new HttpClient();
var uri = new Uri("<SERVER_HOST_NAME>/api/message/picture");
Photo p = new Photo
{
//set four int variables and read an image into a byte[] from a file
};
var content = new StringContent(JsonConvert.SerializeObject(p), Encoding.UTF8, "application/json");
HttpResponseMessage resp = null;
resp = await client.PostAsync(uri, content);
//do stuff with the response
这是我在WebApiConfig中的路由:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}/{id2}/{id3}",
defaults: new { id = RouteParameter.Optional, id2 = RouteParameter.Optional, id3 = RouteParameter.Optional }
);
我想要访问的功能:
[HttpPost, ActionName("picture")]
public void AddPicture([FromBody] Photo p)
{
//Call two methods on the photo
}
目前,这给了我404错误。当我将字符串内容更改为随机字符串时,它会找到该函数但返回500,因为随机字符串无法转换为Photo。我尝试使用MultipartContent
和MultipartFormDataContent
两者使用完全相同的结果。我很茫然,因为我有其他功能与这个功能非常相似并且工作得很好。我做错了什么?