简体   繁体   English

指定如何在REST服务中传递参数

[英]Specify How Parameters are Passed in REST service

I'm planning to create a REST service using C#, but am a little confused how to structure it. 我打算使用C#创建REST服务,但如何构造它却有些困惑。

In this basic example, there is one method that should write TimeSeries data. 在此基本示例中,有一种方法应该写入TimeSeries数据。 From the research I have done, I expect the URL to resemble: http://myserver/v1/TimeSeries/ {id} 根据我所做的研究,我希望URL类似于: http:// myserver / v1 / TimeSeries / {id}

Example: http://myserver/v1/timeseries/1 {["20180101","10"]["20180102","20"]} 示例: http:// myserver / v1 / timeseries / 1 {[“ 20180101”,“ 10”] [“ 20180102”,“ 20”]}

In this example, the TimeSeries ID is 1 and the JSON (maybe not correct JSON, but illustrates example) represents the data points to be written. 在此示例中,TimeSeries ID为1,并且JSON(可能不是正确的JSON,但示出了示例)表示要写入的数据点。

So the ID of the time series to write is in the URI. 因此,要写入的时间序列的ID在URI中。 The actual data to be written would be in the request body (posted as JSON). 要写入的实际数据将在请求正文中(发布为JSON)。

Here's what I have so far: 这是我到目前为止的内容:

[ServiceContract]
public interface ITimeSeriesService
{
    [OperationContract]
    [WebInvoke(Method = "PUT", UriTemplate = "timeseries", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string WriteTimeSeries(int id, string datapoints);

}

So my questions are: 所以我的问题是:

  • How do I bind the method to the URI as described above? 如上所述,如何将方法绑定到URI?
  • How do I specify that parameter 'id' is in the URI, whilst 'datapoints' is in the body? 如何在URI中指定参数“ id”,而在正文中指定参数“ datapoints”呢?

I am using .Net 4.5.2 我正在使用.Net 4.5.2

Thanks for any assistance. 感谢您的协助。

JSON is just a string representation of what in a an Object Oriented language would be a class and it's properties and fields. JSON只是一种字符串表示形式,它表示在面向对象的语言中什么是类,并且是属性和字段。

For example in C# you might have a class like: 例如,在C#中,您可能有一个类似的类:

Animal
{
   public string Breed {get; set:}
   public int Age {get; set;}
}

the JSON to pass an animal to your (assuming Web API ) controller method would look like: 将动物传递到您的(假设Web API)控制器方法的JSON看起来像:

{"Animal":{"Breed":"Bull Dog", "Age":"5"}}

and in your WebAPI controller using default routing ( {controller}/{action} ) your method would look something like: 在使用默认路由( {controller}/{action} )的WebAPI控制器中,您的方法将类似于:

public string AddDog([FromBody]Animal animal)
{
   // do stuff with animal
}

Most likely with the above I'd expect a POST method with the JSON in the body of the request. 与上述最有可能的是,我希望在请求的主体中使用JSON的POST方法。 WebAPI / MVC will try to route the method based on what best matches the request. WebAPI / MVC将尝试根据最符合请求的路由来路由该方法。

The URL/ Query would look something like: URL /查询如下所示:

 http://myApp:4000/Animal/Add

Of course, if you are building this to be used with another .NET App, you would just use HttpClient. 当然,如果要构建它以与另一个.NET App一起使用,则只需使用HttpClient。 That code would look much like: 该代码看起来像:

// .net core 2 with extensions
var Client = new HttpClient();
var message = client.PostAsJsonAsync("http://myApp:4000/Animal/Add", myAnimalObject).GetAwaiter().GetResult();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM