支持GET *和*中的WebAPI POST [英] Supporting GET *and* POST in WebApi

查看:156
本文介绍了支持GET *和*中的WebAPI POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们来测试模型。

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

我想这项服务能够接受以下要求:

I would like this service to be able to accept the following requests:


  • GET /测试号= 1234&放大器;文本= MYTEXT

  • POST /测试与头:内容类型:应用程序/ x-WWW的形式urlen codeD 和身体:数= 1234&安培;文字= MYTEXT

  • POST /测试与头:内容类型:应用程序/ JSON 和身体: {文本:提供,编号: 9876}

  • GET /test?Number=1234&Text=MyText
  • POST /test with header: Content-Type: application/x-www-form-urlencoded and body: Number=1234&Text=MyText
  • POST /test with header: Content-Type: application/json and body: {"Text":"Provided!","Number":9876}

的路由进行配置的方式如下:

The routes are configured the following way:

_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" });

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

My controller looks like this:

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

    [HttpPost]
    public TestResponseModel Post([FromBody] TestRequestModel model)
    {
       return Do(model);
    }
    (...)

这似乎是锅炉板code可接受量的,但我还是想尽可能地避免它。

This seems like an acceptable amount of boiler plate code, but I still would like to avoid it if possible.

有额外的路线不理想了。我有一个恐惧的MVC /的​​WebAPI的路线,我相信他们是邪恶的。

Having the extra route is not ideal too. I have a fear of MVC/WebAPi routes and I believe they are evil.

有没有办法来避免两种方法和/或 DefaultPost 的路线?

Is there a way to avoid having two methods and/or the DefaultPost route?

推荐答案

你所要求的是不典型用的ASP.NET Web API。 ASP.NET MVC中,是很常见的具有相同的操作方法处理的初始GET和随后的交背面(POST)。的ASP.NET Web API的目的是为建立HTTP服务和GET用于检索资源而不在系统中进行任何更改,而POST是创建一个新的资源,马修的指出。

What you are asking for is not typical with ASP.NET Web API. In ASP.NET MVC, it is common to have the same action method handling the initial GET and the subsequent post back (POST). ASP.NET Web API is intended for building HTTP services and GET is used for retrieving a resource without changing anything in the system, while POST is for creating a new resource, as pointed by Matthew.

总之,这不是不可能有在网页API一个动作的方法来做到这一点。但你想同样的操作方法,不仅处理GET和POST,并做模型绑定和格式化结合。模型绑定(类似于MVC)结合请求的URI,查询字符串等为参数,同时结合格式化(独特的网络API)结合的主体内容为参数。默认情况下,简单的类型从URI,查询字符串和复杂类型从身体的约束。所以,如果你有一个动作方法与参数字符串文本,整型数字,TestRequestModel模型,则可以从URI或机构,在这种情况下,有专用的网络API绑定,你会需要检查什么不为空并使用它。但是,这样的解决方案看起来更像是一个黑客,很遗憾。或者,如果你想的一样复杂类型从两个URI /查询字符串和身体被填充,则需要相应地编写您自己的参数粘结剂来检查请求部分和填充参数。

Anyway, it is not impossible to have one action method in Web API to accomplish this. But you want the same action method to not only handle GET and POST and also do the model binding and formatter binding. Model binding (similar to MVC) binds request URI, query string, etc to parameters while formatter binding (unique to web API) binds the body content to parameter. By default, simple types are bound from URI, query string and complex types from body. So, if you have an action method with parameters of string text, int number, TestRequestModel model, you can have web API bind from URI or body and in this case, you will need to check what is not empty and use that. But, such a solution will look more like a hack, unfortunately. Or if you want the same complex type to be populated from both URI/query string and body, you will need to write your own parameter binder that checks for request parts and populate the parameter accordingly.

此外,你不需要有两个路径映射。这样默认的就可以了。

Also, you don't need to have two route mappings. The default one like this will do.

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

这篇关于支持GET *和*中的WebAPI POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆