[英]ASP.NET WebApi not receiving post data
I need to receive some string and binary data using WebApi.我需要使用 WebApi 接收一些字符串和二进制数据。 I have created a controller this way:
我以这种方式创建了一个控制器:
[HttpPost]
public void Post(byte[] buffer)
{
// Some code goes here
}
Here's the routtings:这是路由:
routes.MapHttpRoute(
name: "CuscarD95B",
routeTemplate: "D95B/{controller}",
defaults: new { buffer = RouteParameter.Optional },
constraints: new { controller = @"Cuscar" }
Now when I try to post some data, buffer is always byte[0] (zero length array).现在,当我尝试发布一些数据时,缓冲区始终是 byte[0](零长度数组)。 No data is being passed to the controller.
没有数据被传递到控制器。
Any help would be appreciated.任何帮助,将不胜感激。
Thanks.谢谢。
If you are ever struggling with deserializing a body, try and do it manually to see if you are actually sending it correctly.如果您曾经在反序列化一个 body 方面遇到困难,请尝试手动进行,看看您是否真的正确发送了它。
[HttpPost]
public void Post()
{
string body = Request.Content.ReadAsStringAsync().Result;
}
We passed Json object by HttpPost method, and parse it in dynamic object.我们通过 HttpPost 方法传递 Json 对象,并在动态对象中解析它。 it works fine.
它工作正常。 this is sample code:
这是示例代码:
ajaxPost:
...
Content-Type: application/json,
data: {"name": "Jack", "age": "12"}
...
Web API:网络API:
[HttpPost]
public string DoJson2(dynamic data)
{
string name = data.name;
int age = data.age;
return name;
}
If there is complext object type, you need to parse the dynamic type by using:如果有复杂对象类型,则需要使用以下方法解析动态类型:
JsonConvert.DeserializeObject< YourObjectType >(data.ToString());
A complex data content sample is here, it includes array and dictionary object:一个复杂的数据内容示例在这里,它包括数组和字典对象:
{"AppName":"SamplePrice","AppInstanceID":"100","ProcessGUID":"072af8c3-482a-4b1c-890b-685ce2fcc75d","UserID":"20","UserName":"Jack","NextActivityPerformers":{"39c71004-d822-4c15-9ff2-94ca1068d745":[{"UserID":10,"UserName":"Smith"}]}}
I was able to post a byte[] in my request body, and the value was successfully model bound.我能够在我的请求正文中发布一个字节 [],并且该值已成功绑定模型。 Was the content-type set?
是否设置了内容类型? Note that simple string is considered to be valid JSON here, and so it should work if your request's content-type is set to application/json...
请注意,简单字符串在这里被认为是有效的 JSON,因此如果您的请求的内容类型设置为 application/json ,它应该可以工作...
Having said that, you can simply have your POST method that expects a string from the body instead of a byte[] and set the content-type to be application/json:话虽如此,您可以简单地使用 POST 方法,该方法需要来自正文的字符串而不是 byte[],并将内容类型设置为 application/json:
[HttpPost]
public void Post([FromBody]string buffer)
{
// Some code goes here
}
我不确定如何将byte[]
发送到 webApi,但您可以做的是将二进制数据作为Base64 string
传递给您的控制器,然后将base64 string
转换为byte[]
。
您是否尝试在没有[FromBody]
情况下使用 HttpPostedFileBase ?
public void Post(HttpPostedFileBase buffer)
I had a similar situation where I was using a string array and it would always come through null or I would get an exception about an improperly formatted request.我遇到过类似的情况,我使用的是字符串数组,它总是通过 null 出现,否则我会收到有关格式不正确的请求的异常。 I added the annotation
[FromUri]
in my method parameters then it worked.我在我的方法参数中添加了注释
[FromUri]
然后它起作用了。 To give a little more context, my controller method looked like this before:为了提供更多上下文,我的控制器方法以前看起来像这样:
public async Task<IHttpActionResult> MyControllerName(List<string> id)
After:后:
public async Task<IHttpActionResult> MyControllerName([FromUri] List<string> id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.