【发布时间】:2018-04-18 10:14:52
【问题描述】:
在我的 WebApi 中,我有一个 HttpGet 和 HttpPost 方法,get 方法工作正常,并且调用了 post 方法,但正文内容始终为空,除非在 HttpRequestMessage 中使用。我尝试以字符串格式(首选数据类型)以及模型提供正文内容,但这些方法都不起作用。我也尝试过切换内容类型但没有成功。有谁知道我是否做错了什么,或者我如何轻松地从HttpRequestMessage 获取变量数据,在下面的示例中是“测试”。
方法一:
[System.Web.Http.HttpPost]
[Route("api/v1/AddItem")]
public IHttpActionResult AddItem([FromBody]string filecontent, string companycode)
{
MessageBox.Show(filecontent);
Return Ok("");
}
方法2(带模型):
[System.Web.Http.HttpPost]
[Route("api/v1/AddItem")]
public IHttpActionResult AddItem([FromBody]ItemXML filecontent, string companycode)
{
MessageBox.Show(filecontent.XMLContent);
Return Ok("");
}
型号:
public class ItemXML
{
public ItemXML(string content)
{
XMLContent = content;
}
public string XMLContent { get; set; }
}
方法三:
[System.Web.Http.HttpPost]
[Route("api/v1/AddItem")]
public IHttpActionResult AddItem(HttpRequestMessage filecontent, string companycode)
{
var content = filecontent.Content.ReadAsStringAsync().Result;
MessageBox.Show(content);
Return Ok("");
}
方法3内容字符串(“test”是提供的值):“content”------WebKitFormBoundarydu7BJizb50runvq0\r\nContent-Disposition: form-data; name=\"filecontent\"\r\n\r\n\"test\"\r\n------WebKitFormBoundarydu7BJizb50runvq0--\r\n" string"
【问题讨论】:
-
显示请求是如何产生的。您是在发布 JSON 还是表单数据?由于文件内容参数名称而采用形式
-
我尝试了两种方法,但是没有成功
标签: c# asp.net-web-api swagger postman