的ASP.NET Web API POST参数为null [英] ASP.NET Web API POST parameter is null

查看:204
本文介绍了的ASP.NET Web API POST参数为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(为了澄清,我看过其他的问题进行解答,并试图所有的解决方案,但没有人解决这个问题)

(To clarify, I have read other questions and their answers and tried all solutions, but none of them solved the issue)

我在本地调试code,我将部署到Windows Azure云服务。它是正在短短数天前的ASP.NET Web API应用,但现在不是了。

I'm debugging locally a code that I'll deploy to Windows Azure Cloud Services. It's an ASP.NET Web API application that was working just a few days ago, but not anymore.

我有GET和POST处理的控制器。获取处理程序工作正常,并返回所需的结果,但是POST处理器是不是能够得到来自POST请求体中的参数。该处理程序的工作就好了几构建前(〜2天前),但它不工作了。下面是详细信息:

I have a controller with both GET and POST handlers. The GET handler works fine and returns the desired result, however the POST handler is not able to get the parameters from the POST body request. This handler was working just fine a few builds ago (~2 days ago), however it's not working now. Here are the details:

首先,GET和POST处理程序:

First, both GET and POST handlers:

public class PlayerController : ApiController
{
    [HttpGet]
    public HttpResponseMessage Information(string userID)
    {
        // Works fine!
    }

    [HttpPost]
    public HttpResponseMessage Insert(PlayerEntry entry)
    {
        // All values from entry are null or 0!
    }
}

这是 PlayerEntry 模型:

public class PlayerEntry
{
    public string userID;
    public string userName;
    public int userProgress;
}

这是正在进行的POST请求(从小提琴手采取):

This is the POST request being made (taken from Fiddler):

POST http://127.0.0.1/api/player/insert HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 80
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

userID=20&userName=Test&userProgress=9

这是在服务器的调试输出:

And this is the Debug Output in the server:

iisexpress.exe Information: 0 : Request, Method=POST, Url=http://127.0.0.1:81/api/player/insert, Message='http://127.0.0.1:81/api/player/insert'
iisexpress.exe Information: 0 : Message='Player', Operation=DefaultHttpControllerSelector.SelectController
iisexpress.exe Information: 0 : Message='MvcWebRole1.Controllers.PlayerController', Operation=DefaultHttpControllerActivator.Create
iisexpress.exe Information: 0 : Message='MvcWebRole1.Controllers.PlayerController', Operation=HttpControllerDescriptor.CreateController
iisexpress.exe Information: 0 : Message='Selected action 'Insert(PlayerEntry entry)'', Operation=ApiControllerActionSelector.SelectAction
iisexpress.exe Information: 0 : Message='Value read='MvcWebRole1.Models.PlayerEntry'', Operation=JQueryMvcFormUrlEncodedFormatter.ReadFromStreamAsync
iisexpress.exe Information: 0 : Message='Parameter 'entry' bound to the value 'MvcWebRole1.Models.PlayerEntry'', Operation=FormatterParameterBinding.ExecuteBindingAsync
iisexpress.exe Information: 0 : Message='Model state is valid. Values: entry=MvcWebRole1.Models.PlayerEntry', Operation=HttpActionBinding.ExecuteBindingAsync
iisexpress.exe Information: 0 : Message='Action returned 'StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
  Content-Type: text/plain; charset=utf-8
}'', Operation=ReflectedHttpActionDescriptor.ExecuteAsync
iisexpress.exe Information: 0 : Operation=ApiControllerActionInvoker.InvokeActionAsync, Status=200 (OK)
iisexpress.exe Information: 0 : Operation=PlayerController.ExecuteAsync, Status=200 (OK)
iisexpress.exe Information: 0 : Response, Status=200 (OK), Method=POST, Url=http://127.0.0.1:81/api/player/insert, Message='Content-type='text/plain; charset=utf-8', content-length=0'
iisexpress.exe Information: 0 : Operation=PlayerController.Dispose

到目前为止,这些都是我试过的事情,没有任何改变:

So far, these are the things I've tried that didn't change anything:


  • 添加一个=,在POST正文的开头(只是要确定它不会工作)

  • 添加在插入 [FromBody] PlayerEntry条目()功能

  • 尝试测试其他任何POST处理程序我在其他控制器(他们没有工作!)

然而,当我只需删除 PlayerEntry 模型,并从URL中的POST参数(并通过URL发送参数。我这样做,只是作为测试) ,我可以得到所有的参数没有问题。

However, when I simply remove the PlayerEntry model and get the POST parameters from the URL (and send the parameters through the URL. I did that only as a test), I can get all the parameters without problems.

有什么能在这里造成的问题?我记得(在内部运行,并连接到SQL Server和返回结果只有code),和所有突然我得到了这个问题,我并没有改变在处理任何code远。

What could be causing the issue here? I did not change any code in the handlers as far as I remember (only code that runs internally and connects to an SQL Server and returns results), and all of a sudden I got this problem.

推荐答案

我相信默认的模型绑定不会绑定到的字段的,只有的属性的与公共二传手。试着改变你的字段属性:

I believe the default model binder does not bind to fields, only properties with a public setter. Try changing your fields to properties:

public class PlayerEntry
{
    public string userID { get; set; }
    public string userName { get; set; }
    public int userProgress { get; set; }
}

这篇关于的ASP.NET Web API POST参数为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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