我今天用很多时间用POST( HttpClient.PostAsync )方法调用web api函数。但不幸的是我做不到。 只有使用GET( HttpClient.GetAsync )方法的调用才能成功。 我尝试在网上跟踪许多样本,但总是出现同样的错误。 (“未找到”)
非常感谢有人可以帮助我
以下是C#Web API:
[RoutePrefix("NewAreaMap")]
public class NewAreaMapController: ApiController
{
[HttpPost]
[ActionName("PostCreateAreaTemp")]
public AreaTemp PostCreateAreaTemp(double southLatitude, double westLongitude, double northLatitude, double eastLongitude, int countryId, int worldId)
{
AreaTemp newTempMap = new AreaTemp();
//.....
* * Here is the C# code from client side: * *
using(var client = new HttpClient())
{
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["SrvWebApiPath"].ToString());
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var values = new Dictionary < string,
string > ()
{
{
"southLatitude", southLatitude.ToString()
},
{
"westLongitude", westLongitude.ToString()
},
{
"northLatitude", northLatitude.ToString()
},
{
"eastLongitude", eastLongitude.ToString()
},
{
"countryId", countryId.ToString()
},
{
"worldId", worldId.ToString()
}
};
var content = new FormUrlEncodedContent(values);
HttpResponseMessage response = await client.PostAsync("api/NewAreaMap/PostCreateAreaTemp", content)
if (response.IsSuccessStatusCode)
{
string jsonData = response.Content.ReadAsStringAsync().Result;
newAreTemp = JsonConvert.DeserializeObject < AreaTemp > (jsonData);
}
}
GET调用适用于以下网址:
HttpResponseMessage response = await client.GetAsync("api/NewAreaMap/GetAreaTemp/?latitudeAreaCenter=7.02&longitudeAreaCenter=9.05");
答案 0 :(得分:1)
将您的方法参数替换为object,因为您传递的是完整对象 来自httpclient的“content”所以在这种情况下你需要在[frombody]属性中使用相同的对象 methodname([FromBody]内容内容) 定义一个类中的所有属性并使用。希望它对你有所帮助。
答案 1 :(得分:1)
由于您发布了JSON,因此您也可以将其作为对象发送。或者,如果您仍想保留字典和方法的签名,您可以尝试:
var content = new StringContent(JsonConvert.SerializeObject(values),
Encoding.UTF8, "application/json");
而不是
var content = new FormUrlEncodedContent(values);
这是一个带对象的例子。
public class SampleObject
{
public double SouthLatitude { get; set; }
public double WestLongitude { get; set; }
public double NorthLatitude { get; set; }
public double EastLongitude { get; set; }
public int CountryId { get; set; }
public int WorldId { get; set; }
}
并更改您的请求。
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["SrvWebApiPath"].ToString());
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var obj = new SampleObject
{
SouthLatitude = southLatitude,
WestLongitude = westLongitude,
NorthLatitude = northLatitude,
EastLongitude = eastLongitude,
CountryId = countryId,
WorldId = worldId
};
// Send it as StringContent.
var request = new StringContent(JsonConvert.SerializeObject(obj),
Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("api/NewAreaMap/PostCreateAreaTemp", request)
if (response.IsSuccessStatusCode)
{
string jsonData = response.Content.ReadAsStringAsync().Result;
newAreTemp = JsonConvert.DeserializeObject<AreaTemp>(jsonData);
}
}
服务器上的签名。
public AreaTemp PostCreateAreaTemp(SampleObject sampleObject)
或者如果需要:
public AreaTemp PostCreateAreaTemp([FromBody]SampleObject sampleObject)
答案 2 :(得分:0)
请尝试将 FromBody 属性与您的操作参数一起使用。