在 ASP.NET 4.5 和 VS 2012 RC 中将字符串发布到 Web API 控制器 [英] POST a string to Web API controller in ASP.NET 4.5 and VS 2012 RC
本文介绍了在 ASP.NET 4.5 和 VS 2012 RC 中将字符串发布到 Web API 控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 WebAPI 的新手,正在尝试学习它.我有一个 WebAPI 控制器,我正在尝试使用单元测试中的 WebClient 向其发送字符串.
I am new to WebAPI and trying to learn it. I have an WebAPI controller to which I am trying to POST a string using WebClient from my Unit Test.
我正在使用以下代码将字符串发布到我的 WebAPI.
I am posting a string to my WebAPI using following code below.
using (var client = new WebClient())
{
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var result = client.UploadString(_webapiUrl, "POST", "hello");
}
这是我的控制器.
[HttpPost]
public byte[] Post(string value)
{
// Do something with value
}
我可以在我的控制器上遇到一个断点,但它似乎没有 POST 任何字符串,而且我总是得到 NULL 值.我应该怎么做才能获得价值?
I can hit a break point on my controller, but it doesn't seem to POST any string and I always get NULL value. What should I do to get the value ?
谢谢
推荐答案
如果您只需要接收一个值,请在该值前使用 =
:
If all you need to receive is one value, use =
before the value:
var result = client.UploadString(_webapiUrl, "POST", "=hello"); // NOTE '='
这篇关于在 ASP.NET 4.5 和 VS 2012 RC 中将字符串发布到 Web API 控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文