Reputation: 806
I'm planning to create a REST service using C#, but am a little confused how to structure it.
In this basic example, there is one method that should write TimeSeries data. From the research I have done, I expect the URL to resemble: http://myserver/v1/TimeSeries/{id}
Example: 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.
So the ID of the time series to write is in the URI. The actual data to be written would be in the request body (posted as 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:
I am using .Net 4.5.2
Thanks for any assistance.
Upvotes: 0
Views: 89
Reputation: 5140
JSON is just a string representation of what in a an Object Oriented language would be a class and it's properties and fields.
For example in C# you might have a class like:
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:
{"Animal":{"Breed":"Bull Dog", "Age":"5"}}
and in your WebAPI controller using default routing ({controller}/{action}
) your method would look something like:
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. WebAPI / MVC will try to route the method based on what best matches the request.
The URL/ Query would look something like:
http://myApp:4000/Animal/Add
Of course, if you are building this to be used with another .NET App, you would just use 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();
Upvotes: 1