我应该使用" FromBody] QUOT;值或自定义PARAMS响应时,在Web API应用程序的HTTP POST电话? [英] Should I use "[FromBody]" values or custom params when responding to an HTTP Post call in a Web API app?

查看:335
本文介绍了我应该使用" FromBody] QUOT;值或自定义PARAMS响应时,在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?

我想从一个客户端发送一个URL(Windows窗体)应用程序/ UTIL到我的网页API的应用程序告诉它通过检索存储的过程数据,然后将结果保存在本地表。没有数据返回给调用者,它只是一个通知做一些工作。

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.

要prepare对于这一点,我添加了一个新的途径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 }
);

在控制器,这种方法已经存在(自动添加):

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.
}

这是做正确的/更好的办法,或者是preferable拉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?

推荐答案

选择您的行动的权利动词的事情总是有一些值得商榷。如果你看一下 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方法请求目标资源的过程的结果
  重新presentation根据资源的结果,封闭在请求
  自己特定的语义。例如,在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;

中的关键字是粗体亮点。正如你可以看到它完全适合你的情况,你发送的数据的某些阻止有的过程是由您的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.

由于此事的如何处理在您的文章中的参数即可。您可以创建一个DTO映射您的字段:设备,BEGINDATE,结束日期。既然你是从身体发出你的参数,会的WebAPI使用格式化绑定的参数。该的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)

PriceComplianceDTO

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

这篇关于我应该使用" FromBody] QUOT;值或自定义PARAMS响应时,在Web API应用程序的HTTP POST电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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