Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从POST方法检索数据时获取异常?_C#_Asp.net_Asp.net Web Api_Asp.net Web Api2 - Fatal编程技术网

C# 从POST方法检索数据时获取异常?

C# 从POST方法检索数据时获取异常?,c#,asp.net,asp.net-web-api,asp.net-web-api2,C#,Asp.net,Asp.net Web Api,Asp.net Web Api2,我在读取post数据时遇到异常。 我在这一行中遇到错误: HttpContext.Current.Request.Form["UserID"].ToString(); 错误是: System.Collections.Specialized.NameValueCollection.this[string]。获取 返回null 在方法中,我输入了以下代码: StreamReader reader = new StreamReader(HttpContext.Current.Request.Inpu

我在读取post数据时遇到异常。 我在这一行中遇到错误:

HttpContext.Current.Request.Form["UserID"].ToString();
错误是:

System.Collections.Specialized.NameValueCollection.this[string]。获取 返回null

在方法中,我输入了以下代码:

StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream);
string requestFromPost = reader.ReadToEnd();
数据的输入方式如下:

{
  "UserID": "1000",
  "Password": "ABCD"
}
为什么我没有在这个
HttpContext.Current.Request.Form[“UserID”].ToString()中获取值?我也尝试了Request.QueryString,但在这里没有成功。

我哪里做错了?任何帮助或建议都将不胜感激。谢谢

此请求中没有
表格
。要将请求正文解释为表单数据,它必须:

  • 内容类型为
    x-www-form-urlencoded
  • 实际格式为表单编码值,即
    UserID=foo&Password=bar
JSON内容是JSON,它不会被解释为表单数据

Web API应该已经为您解决了这一问题。给定一个动作方法:

公共作废操作(凭证)

其中,
凭证
类类似于:

public class Credentials 
{
    string UserID { get; set;}
    string Password { get; set; }
}

您不必做任何其他事情,就可以让框架将这个传入的JSON数据转换为
凭证的实例
,并将其传递给action方法。这是自动的,除非您做了一些奇怪的事情,打破了WebAPI期望的惯例。

此请求没有
表单。要将请求正文解释为表单数据,它必须:

  • 内容类型为
    x-www-form-urlencoded
  • 实际格式为表单编码值,即
    UserID=foo&Password=bar
JSON内容是JSON,它不会被解释为表单数据

Web API应该已经为您解决了这一问题。给定一个动作方法:

公共作废操作(凭证)

其中,
凭证
类类似于:

public class Credentials 
{
    string UserID { get; set;}
    string Password { get; set; }
}

您不必做任何其他事情,就可以让框架将这个传入的JSON数据转换为
凭证的实例
,并将其传递给action方法。这是自动的,除非你做了一些奇怪的事情,打破了WebAPI所期望的惯例。

可能重复的@AFriend与该链接不同。@AFriend可能重复的地方与该链接不同。谢谢你的回答,但是我如何访问我的方法中的值?你为什么直接从是否仍在WebAPI项目中请求?模型绑定器应该根据传入的请求数据为您创建操作方法参数。我建议你读一本WebAPI教程,有成千上万的。谢谢@Tom。这真的很有帮助。我使用了
JObject
实现了这一点,但这是一种错误的方法。我是这样做的:
var obj=JObject.Parse(requestFromPost)JObject
实现了这一点,但这是一种错误的方法。我是这样做的:
var obj=JObject.Parse(requestFromPost)