帖子参数始终为空 [英] Post parameter is always null

查看:32
本文介绍了帖子参数始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从为 WebAPI 升级到 RC 后,我在 WebAPI 上调用 POST 时遇到了一些非常奇怪的问题.我什至回到了在新项目上生成的基本版本.所以:

Since upgrading to RC for WebAPI I'm having some real odd issue when calling POST on my WebAPI. I've even gone back to the basic version generated on new project. So:

public void Post(string value)
{
}

并从 Fiddler 调用:

and calling from Fiddler:

Header:
User-Agent: Fiddler
Host: localhost:60725
Content-Type: application/json
Content-Length: 29

Body:
{
    "value": "test"
}

当我调试时,字符串value"从未被分配到.它总是NULL.有人遇到这个问题吗?

When I debug, the string "value" is never being assigned to. It's just always NULL. Anyone having this issue?

(我第一次看到一个更复杂类型的问题)

(I first saw the issue with a more complex type)

此问题不仅限于 ASP.NET MVC 4,RC 安装后新的 ASP.NET MVC 3 项目也会出现同样的问题

The problem is not only bound to ASP.NET MVC 4, the same problem occurs for a fresh ASP.NET MVC 3 project after RC installation

推荐答案

因为你只有一个参数,你可以尝试用 [FromBody] 属性装饰它,或者改变方法接受一个将值作为属性的 DTO,正如我在此处建议的:MVC4 RC WebApi 参数绑定

Since you have only one parameter, you could try decorating it with the [FromBody] attribute, or change the method to accept a DTO with value as a property, as I suggested here: MVC4 RC WebApi parameter binding

更新:官方 ASP.NET 网站今天更新了一个很好的解释:https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-1

UPDATE: The official ASP.NET site was updated today with an excellent explanation: https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-1

简而言之,当在正文中发送单个简单类型时,只发送以等号 (=) 为前缀的值,例如身体:

In a nutshell, when sending a single simple type in the body, send just the value prefixed with an equal sign (=), e.g. body:

=test

这篇关于帖子参数始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆