Web API HttpDelete-如何在[FromBody]中调用delete API方法并发送模型

问题描述

我正在尝试通过在url中发送参数来调用HttpDelete宁静的Web API方法。通话成功。

示例

string url = path + "deletemethodName?Id=" + Convert.ToInt32(idpamameter) + "&name=" + nameParameter;
                using (HttpClient client = new HttpClient())
                {
                    using (HttpResponseMessage response = client.DeleteAsync(url).Result)
                    {
                        if (response.IsSuccessstatusCode)
                        {
                            return Json(response.Content.ReadAsstringAsync().Result);
                        }
                       }
                }

有没有一种方法-在调用HttpDelete方法时,就像我们用来在体内发送模型数据并通过在API方法中使用[FromBody]调用POST方法那样,访问模型数据而不是在网址?

由于我的HttpDelete方法中有5个以上的参数要附加在URL中-我希望发送模型数据。

谢谢。

解决方法

更高级别的DeleteAsync不支持主体,但是我们可以“漫长地”做到这一点:

var request = new HttpRequestMessage {
    Method = HttpMethod.Delete,RequestUri = new Uri("http://mydomain/api/something"),Content = new StringContent(JsonConvert.SerializeObject(myObj),Encoding.UTF8,"application/json")
};
var response = await client.SendAsync(request);
,

您可以按发布方法删除记录。 如果您以表格格式显示记录,那么我们的Foreach循环以及在该循环内使用Html Form如下:

正在使用(Html.BeginForm(“ ACTIONNAME”,“ CONTROLLERNAME”,新的{id = item.id,.....}))

然后添加

df <- structure(list(Mno = c(100003L,100003L,100045L,100202L,100202L),Record_date = c("15-05-2004","09-12-2009","28-04-2012","29-01-2011","14-11-2006","15-10-2009"),Fv_Date = c("26-09-2003","26-09-2003","27-09-2003","02-10-2003","02-10-2003"
)),class = "data.frame",row.names = c(NA,-6L))

用于将数据发布到动作控制器。