【发布时间】:2017-10-11 11:56:03
【问题描述】:
我正在使用带有 VisualStudio 2017 社区的 asp.net core 1.1 开发一个 Web 应用程序,在这个应用程序中我使用一个 web-api 服务:
public static T SendPostRequest<T>(string pController, T pParam){
using (HttpClient client = new HttpClient()){
string uri = API_URL + pController;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var jsonInString = JsonConvert.SerializeObject((T)pParam);
var content = new StringContent(jsonInString, Encoding.UTF8, "application/json");
var result = client.PostAsync(uri, content).Result;
return JsonConvert.DeserializeObject<T>(result.Content.ReadAsStringAsync().Result);
}
}
当此代码到达 api 控制器时,我的对象为空,但 jsonInString 具有我的对象的值。 当我使用 PostMan,使用 raw 和 JSON(applications/json),如果你看到我的对象的属性是空值,同样的错误通过了
但是,如果我使用带有表单数据的邮递员,在我的 apicontroller 中我接收对象的数据属性,我需要你的帮助,当我从我的 asp.net 核心网络中的代码发送时,我如何接收我的对象的值,希望对你有所帮助
【问题讨论】:
-
更改
FromForm->FromBody -
是的!只需将 FromForm 更改为 FromBody
标签: c# asp.net-web-api asp.net-mvc-5 asp.net-core asp.net-web-api2