支持WebApi中的GET *和* POST

我们有一个测试模型。

public class TestRequestModel { public string Text { get; set; } public int Number { get; set; } } 

我希望这项服务能够接受以下请求:

  • GET / test?Number = 1234&Text = MyText
  • POST / test with header: Content-Type:application / x-www-form-urlencoded and body: Number = 1234&Text = MyText
  • 带标题的POST / testContent-Type:application / json和body: {“Text”:“提供!”,“数字”:9876}

路由配置方式如下:

 _config.Routes.MapHttpRoute( "DefaultPost", "/{controller}/{action}", new { action = "Post" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }); _config.Routes.MapHttpRoute( "The rest", "/{controller}/{action}", defaults: new { action = "Get" }); 

我的控制器看起来像这样:

 public class TestController : ApiController { [HttpGet] public TestResponseModel Get([FromUri] TestRequestModel model) { return Do(model); } [HttpPost] public TestResponseModel Post([FromBody] TestRequestModel model) { return Do(model); } (...) 

这似乎是一个可接受的锅炉板代码量,但我仍然希望尽可能避免它。

拥有额外的路线也不太理想。 我担心MVC / WebAPi路线,我相信它们是邪恶的。

有没有办法避免使用两种方法和/或DefaultPost路由?

您要求的是ASP.NET Web API不常见的。 在ASP.NET MVC中,通常有相同的操作方法处理初始GET和后续回发(POST)。 ASP.NET Web API用于构建HTTP服务,GET用于检索资源而不更改系统中的任何内容,而POST用于创建新资源,如Matthew所指出的。

无论如何,在Web API中使用一个动作方法来实现这一点并非不可能。 但是您希望相同的操作方法不仅可以处理GET和POST,还可以进行模型绑定和格式化程序绑定。 模型绑定(类似于MVC)将请求URI,查询字符串等绑定到参数,而格式化程序绑定(Web API唯一)将主体内容绑定到参数。 默认情况下,简单类型从URI,查询字符串和body中的复杂类型绑定。 因此,如果你有一个带有string text, int number, TestRequestModel model参数的action方法,你可以从URI或body中绑定web API,在这种情况下,你需要检查什么不是空的并使用它。 但是,不幸的是,这样的解决方案看起来更像是黑客。 或者,如果您希望从URI /查询字符串和正文填充相同的复杂类型,则需要编写自己的参数绑定器来检查请求部分并相应地填充参数。

此外,您不需要两个路由映射。 这样的默认值就可以了。

 config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );