ASP.NET的WebAPI没有收到数据后数据、ASP、NET、WebAPI

2023-09-04 00:58:10 作者:徒留一场笑谈一场心伤

我要接收使用的WebAPI一些字符串和二进制数据。我创建了一个控制器是这样的:

I need to receive some string and binary data using WebApi. I have created a controller this way:

    [HttpPost]
    public void Post(byte[] buffer)
    {
        // Some code goes here
    }

这里的routtings:

Here's the routtings:

    routes.MapHttpRoute(
        name: "CuscarD95B",
        routeTemplate: "D95B/{controller}",
        defaults: new { buffer = RouteParameter.Optional },
        constraints: new { controller = @"Cuscar" }

现在,当我尝试发布一些数据,缓冲器总是字节[0](零长度数组)。没有数据被传递到控制器。

Now when I try to post some data, buffer is always byte[0] (zero length array). No data is being passed to the controller.

任何帮助将是AP preciated。

Any help would be appreciated.

感谢。

推荐答案

我找到了解决办法此处。 看来我们需要创建自定义MediaTypeFormatter实现基本类型反序列化。

I found the solution here. Looks like we need to create custom MediaTypeFormatter to achieve primitive type deserialization.