如何使用WebApi将POSTHttpRoute POST到自定义操作?

Dar*_*agh 4 asp.net asp.net-web-api

我试图弄清楚Web API路由背后的疯狂.

当我尝试发布这样的数据时:

curl -v -d "test" http://localhost:8088/services/SendData
Run Code Online (Sandbox Code Playgroud)

我收到404,并出现以下错误消息:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:8088/services/SendData'.","MessageDetail":"No action was found on the controller 'Test' that matches the request."}
Run Code Online (Sandbox Code Playgroud)

这是我的测试服务器的代码.

public class TestController : ApiController
{

    [HttpPost]
    public void SendData(string data)
    {
        Console.WriteLine(data);
    }
}

class Program
{
    static void Main(string[] args)
    {

        var config = new HttpSelfHostConfiguration("http://localhost:8088");

        config.Routes.MapHttpRoute(
            name: "API Default",
            routeTemplate:"services/SendData",
            defaults: new { controller = "Test", action = "SendData"},
            constraints: null);

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

    }
}
Run Code Online (Sandbox Code Playgroud)

更一般地说,为什么ASP.NET团队决定使MapHttpRoute方法如此混乱.为什么需要两个匿名对象....如何知道这些对象实际需要什么属性?

MSDN没有给出任何帮助:http://msdn.microsoft.com/en-us/library/hh835483(v = vs.108).aspx

如果你问我,动态类型语言的所有痛苦都没有任何好处......

Dar*_*rov 8

同意你,这是一个疯狂的地狱,你需要指定data参数应该从POST有效负载绑定,因为Web API自动假定它应该是查询字符串的一部分(因为它是一个简单的类型):

public void SendData([FromBody] string data)
Run Code Online (Sandbox Code Playgroud)

为了让疯狂更加糟糕,你需要在POST前面加载有效负载=(是的,这不是一个错字,它是等号):

curl -v -d "=test" http://localhost:8088/services/SendData
Run Code Online (Sandbox Code Playgroud)

你可以阅读更多关于疯狂的内容this article.

或者停止疯狂并尝试ServiceStack.