我试图找出 Web API 路由背后的疯狂之处。
当我尝试发布这样的数据时:
curl -v -d "test" http://localhost:8088/services/SendData
我收到 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."}
这是我的测试服务器的代码。
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();
}
}
}
更一般地说,为什么 ASP.NET 团队决定让 MapHttpRoute 方法如此混乱。为什么它需要两个匿名对象....任何人都应该知道这些对象实际需要什么属性?
MSDN 没有提供任何帮助: http: //msdn.microsoft.com/en-us/library/hh835483 (v=vs.108).aspx
如果你问我,动态类型语言的所有痛苦都没有任何好处......