什么是不对的ASP.Net和提琴手的例子吗? [英] What is wrong with this ASP.Net and Fiddler example?
问题描述
我使用Visual Studio 2012 RC。我使用的缺省路由,并具有以下的Web API控制器:
I am using Visual Studio 2012 RC. I am using the default routes and have the following Web API controller:
public class FooController : ApiController
{
// GET api/foo
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/foo/5
public string Get(int id)
{
return "value";
}
// POST api/foo
public string Post(string abc)
{
Console.WriteLine("value: {0}", abc);
return "foo" + abc;
}
// PUT api/foo/5
public void Put(int id, string value)
{
}
// DELETE api/foo/5
public void Delete(int id)
{
}
}
我想做POST在提琴手的一个简单的测试,所以我有
I wanted to do a simple test of POST in Fiddler, so I have
请求头
用户代理:提琴手
内容类型:应用程序/ JSON
Request Headers
User-Agent: Fiddler
Content-Type: application/json
请求体
{ABC:DEF}
Request Body
{"abc": "def"}
当我调试的要求,参数ABC进来为空,而不是高清。是不是有什么毛病我提琴手语法?
When I debug the request, the parameter abc comes in as null, not "def". Is there something wrong with my Fiddler syntax?
推荐答案
(1)默认情况下,简单类型从URI拍摄。读取请求主体的简单类型,[FromBody]属性添加到该参数。
(1) By default, simple types are taken from the URI. To read a simple type from the request body, add the [FromBody] attribute to the parameter.
public string Post([FromBody] string abc)
(2){ABC:DEF}定义了一个对象,一个名为abc的属性 - 发送一个JSON字符串,请求主体应该只是DEF
(2) '{"abc": "def"}' defines an object with a property named "abc" - to send a JSON string, the request body should just be "def"
这篇关于什么是不对的ASP.Net和提琴手的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!