我应该使用& quot; [FromBody]& quot;Web API应用程序中响应HTTP Post调用时的值或自定义参数? [英] Should I use "[FromBody]" values or custom params when responding to an HTTP Post call in a Web API app?

查看:117
本文介绍了我应该使用& quot; [FromBody]& quot;Web API应用程序中响应HTTP Post调用时的值或自定义参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

POST是否使用正确的HTTP方法/动词来告诉服务器使用哪些条件来检索数据,然后将其保存到本地?

Is POST the right HTTP method/verb to use for telling the server which criteria to use to retrieve data and then save it locally?

我想从客户端(Windows窗体)应用程序/实用程序向我的Web API应用程序发送URL,以告诉它通过存储的Proc检索数据,然后将结果存储在本地表中.没有数据返回给调用者,这只是做一些工作的通知.

I want to send an URL from a client (Windows Forms) app/util to my Web API app to tell it to retrieve data via a Stored Proc, then store the results in a local table. No data is returned to the caller, it's just a notification to do some work.

为此做准备,我向WebApiConfig添加了一条新路由:

To prepare for this, I added a new route to WebApiConfig:

// Some reports (monthly) only need a begindate, such as "201509"; others need a range 
//  of 2..13 months, such as "2001502" and "201602")
config.Routes.MapHttpRoute(
    name: "ReportsApi",
    routeTemplate: "api/{controller}/{unit}/{begindate}/{enddate}",
    defaults: new { enddate = RouteParameter.Optional }
);

在Controller中,此方法已经存在(已自动添加):

In the Controller, this method already existed (was automatically added):

// POST: api/PriceCompliance
public void Post([FromBody]string value)
{
}

...但是我不知道我是否想要"[FromBody]"爵士乐,所以我添加了这个:

...but I don't know if I want the "[FromBody]" jazz, so I added this:

public void Post(String unit, String beginDate, String endDate)
{
    // TODO: Call the corresponding SP (via a Model or directly here?) and store the results in a table.
}

这是正确/更好的方法吗?还是将URL args从"[FromBody]"中拉出来更可取?实际上,POST甚至是用于这种事情的正确HTTP动词吗?

Is this the right/better way to do it, or is it preferable to pull the URL args out of "[FromBody]"? In fact, is POST even the right HTTP verb to use for this sort of thing?

推荐答案

为操作选择正确的动词始终值得商de.如果您查看 RFC 7231 的4.3.3项:

The matter of choosing the right verb for your action is always something debatable. If you look at the item 4.3.3 of RFC 7231:

POST方法请求目标资源处理
根据资源的
包含在请求中的表示形式自己的特定语义.例如,POST用于以下
功能(以及其他功能):

The POST method requests that the target resource process the
representation enclosed in the request according to the resource's
own specific semantics. For example, POST is used for the following
functions (among others):

  • 提供数据块,例如输入到的HTML表单,以进行数据处理过程
  • Providing a block of data, such as the fields entered into an HTML form, to a data-handling process;

中的关键字以粗体突出显示.如您所见,它非常适合您的情况,您发送一个带有一些数据块的POST请求,并由您的API进行一些进程.

The keywords in that are highlight in bold. As you can see it fits perfectly your case, where you send a POST request with some block of data and some process is made by your API.

关于POST中的如何处理参数.您可以创建一个DTO来映射您的字段: Unit,BeginDate,EndDate .由于您是从正文发送参数,因此WebAPI将使用 Formatter 绑定参数.WebAPI将根据 content-type标头选择更好的格式化程序.在我提供的示例中,使用的格式化程序为JSON.Net(JSON的默认设置)

As matter of how to handle the parameters in your POST. You could create a DTO to map your fields: Unit, BeginDate, EndDate. Since you are sending your parameters from body, WebAPI will bind the parameters using a Formatter. The WebAPI will choose the better formatter to use according to the content-type header. In the example I provided, the formatter used will be the JSON.Net (which is the default for JSON)

价格合规性DTO :

public class PriceComplianceDTO
{
    public string Unit { get; set; }

    public DateTime BeginDate { get; set; }

    public DateTime EndDate { get; set; }
}

控制器:

[RoutePrefix("api/v1")]
public class PriceComplianceController : ApiController
{
    [HttpPost]
    [Route("price")]
    public void Post(PriceComplianceDTO data)
    {
        //Call procedure and process data
    }
}

如果您决定使用上述方法,则也可以从WebApiConfig中删除发布的自定义路由.我刚刚创建了一个新的Web API,这是我的默认路由,可用于POST:

You can also remove the custom route you posted from your WebApiConfig if you decide using the approach above. I just created a new Web API and this is my default route, which is working with the POST:

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

这篇关于我应该使用& quot; [FromBody]& quot;Web API应用程序中响应HTTP Post调用时的值或自定义参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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