自从升级到RC的WebAPI,我有一些真正奇怪的问题时调用POST在我的WebAPI。
我甚至回到了在新项目上生成的基本版本。所以:
public void Post(string value)
{
}
提琴手喊道:
Header:
User-Agent: Fiddler
Host: localhost:60725
Content-Type: application/json
Content-Length: 29
Body:
{
"value": "test"
}
当我调试时,字符串“value”永远不会被赋值。它总是NULL。
有人有这个问题吗?
(我第一次看到这个问题是在一个更复杂的类型上)
这个问题不仅仅局限于ASP。在asp.net MVC 4中,同样的问题出现在一个新的ASP。NET MVC 3项目后RC安装
在Angular中,我可以这样传递数据:
data: '=' + JSON.stringify({ u: $scope.usrname1, p: $scope.pwd1 }),
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' }
在Web API控制器中:
[HttpPost]
public Hashtable Post([FromBody]string jsonString)
{
IDictionary<string, string> data = JsonConvert.DeserializeObject<IDictionary<string, string>>(jsonString);
string username = data["u"];
string pwd = data["p"];
......
或者,我也可以像这样发布JSON数据:
data: { PaintingId: 1, Title: "Animal show", Price: 10.50 }
并且,在控制器中,接受这样的类类型:
[HttpPost]
public string POST(Models.PostModel pm)
{
....
}
两种方式都可以,如果你在API中有一个已经建立的公共类,那么就发布JSON,否则就发布'=' + JSON.stringify({..:…, . .:…})
我有点晚了,但是任何人在使用控制器时偶然发现一个NULL值,只要在POST请求的前面添加“=”就可以了。
当我使用application/json Content-Type时,控制器也传递了一个NULL值。注意下面的“application/x-www-form-urlencoded”内容类型。然而,API的返回类型是“application/json”。
public static string HttpPostRequest(string url, Dictionary<string, string> postParameters)
{
string postData = "=";
foreach (string key in postParameters.Keys)
{
postData += HttpUtility.UrlEncode(key) + "="
+ HttpUtility.UrlEncode(postParameters[key]) + ",";
}
HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myHttpWebRequest.Method = "POST";
byte[] data = System.Text.Encoding.ASCII.GetBytes(postData);
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = data.Length;
Stream requestStream = myHttpWebRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream responseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream, System.Text.Encoding.Default);
string pageContent = myStreamReader.ReadToEnd();
myStreamReader.Close();
responseStream.Close();
myHttpWebResponse.Close();
return pageContent;
}