Web.api, resources and HTTP GET
Now it’s time to set up a Resource that will contain the information we want to pass through our web.api. This resource is a Data Transfer Object (DTO) and is a container to carry our information. It becomes an entity we can use to transfer information with our HTTP calls instead of (possibly many) parameters. Our Task Resource is set up like this:
1. In our WebApi Project, create a new folder called ‘Resources’. In this folder we create a new class called ‘Task’.
2. In the Task class we need the following code to set up our resource entity:
3. Now in our service contract we will set up the endpoint to expose our Resource via GET. I like to think of this service function as the end of a pipe, with messages coming in and DTO’s travelling out when requested. We mark our service function with a WebGet attribute to specify that it can be called using the HTTP GET method.
Using ‘[WebGet(UriTemplate = “”)]’ indicates that the url ‘http://localhost:9000/api/tasks’ will get from this function, as no UriTemplate has been specified. Say we make another WebGet function with the UriTemplate specified, like this:
we can call this function with the url ‘http://localhost:9000/api/tasks/1’.