WebAPI中POST请求参数中有数组问题
webapi接口如下
/// <summary>
/// 删除附件
/// </summary>
/// <param name="pars"></param>
/// <returns></returns>
[HttpPost]
public ResponseResult DeleteAttachment(DeleteAttachmentPars pars)
{
}
DeleteAttachmentPars 实体类 /// <summary>
/// 删除附件参数实体类
/// </summary>
public class DeleteAttachmentPars
{
/// <summary>
/// 路径
/// </summary>
public List<string> DelPathList { get; set; }
}
/// <summary>
/// http请求提交Json数据
/// </summary>
/// <param name="strPostUrl"></param>
/// <param name="strPostData"></param>
/// <returns></returns>
public static string HttpPostJsonData(string strPostUrl, string jsonData)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] b = encoding.GetBytes(jsonData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strPostUrl);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = b.Length;
try
{
using (Stream stream = request.GetRequestStream())
{
stream.Write(b, 0, b.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
using (StreamReader read = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return read.ReadToEnd();
}
}
}
}
catch (Exception ex)
{
LogHelper.Error("在HttpHelper类HttpPostJsonData方法出错", ex);
}
return "";
}
string requestUrl = $"";
DeleteAttachmentPars p = new DeleteAttachmentPars();
p.DelPathList = rootPath;
string resultJson = HttpHelper.HttpPostJsonData(requestUrl, JsonConvert.SerializeObject(p));
ResponseResult result = JsonConvert.DeserializeObject<ResponseResult>(resultJson);
注意:
如果把request.ContentType改为“application/x-www-form-urlencoded”,DeleteAttachment方法接受的参数为null