c#WebApi.SelfHost帖子为空

时间:2017-02-18 22:33:53

标签: c# post asp.net-web-api

我正在使用WebApi.SelfHost,但我的帖子始终为空。 我尝试了所有可以在SO上找到的解决方案,但没有一个能够工作。

我几个小时都遇到了这个问题。 我尝试了所有可以在SO上找到的解决方案,但没有一个能够工作。

我知道这个问题已经被问到了。但我已经尝试了其中的解决方案,但没有一个能够奏效。他们没有使用自我主持人。

这是我的代码。

class Program
{
    static void Main(string[] args)
    {
        var config = new HttpSelfHostConfiguration("http://localhost:8080");

        config.Routes.MapHttpRoute(
            "API Default", "api/{controller}/{action}/{id}",
            new { id = RouteParameter.Optional });

        using (HttpSelfHostServer server = new HttpSelfHostServer(config))
        {
            server.OpenAsync().Wait();
            Console.WriteLine("Press Enter to quit.");
            Console.ReadLine();
        }
    }
}

public class testController : ApiController
{
    [HttpPost]
    public string test(int id ,[FromBody]string value)
    {
        return value;
    }
}

我正在使用Advanced REST client进行测试

这是我的结果

enter image description here

1 个答案:

答案 0 :(得分:2)

您已将字符串值输入到请求的标题部分。您应该在请求正文中发送该值。例如。把标题

Content-Type: application/json

添加正文(在标题部分下方,名为原始有效负载

"testValue"

注意:请考虑使用Postman