Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When and how to use [ResponseType(typeof(...))] correctly?

I am working on a project and I have a basic controller with Get(int id), GetElements(), UpdateElement(int id, Element element), AddElement(Element element) and DeleteElement(int id). Every method use [Route] and [Get]... annotations and returns an IHttpActionResult.

I am confused about [ResponseType(typeof(...))]. What is it for? When and how to use it correctly?

Should I write something like this? [ResponseType(typeof(IEnumerable<Element>))] for GetElements()?

Thanks!

like image 698
Chris Avatar asked Aug 18 '16 09:08

Chris


People also ask

What is responsetype in XMLHttpRequest?

XMLHttpRequest.responseType The XMLHttpRequest property responseType is an enumerated string value specifying the type of data contained in the response. It also lets the author change the response type. If an empty string is set as the value of responseType, the default value of text is used.

When should I set responsetype to a particular value?

When setting responseType to a particular value, the author should make sure that the server is actually sending a response compatible with that format. If the server returns data that is not compatible with the responseType that was set, the value of response will be null. The values supported by responseType are the following:

What is a response type in JavaScript?

The response is a JavaScript object created by parsing the contents of received data as JSON. The response is a text in a DOMString object. This API has not been standardized. The response is part of a streaming download; this response type is only allowed for download requests, and is only supported by Internet Explorer.

What is read-only response in response type?

Response.type The type read-only property of the Response interface contains the type of the response. It can be one of the following: basic: Normal, same origin response, with all headers exposed except “Set-Cookie” and “Set-Cookie2″.


1 Answers

The [ResponseType()] attribute is helpful for creating RESTful Web APIs and also when autogenerating documentation via Swagger / Swashbuckle.

For instance an approach to returning a item based on an id could be written like this

public YourClass Get(int id)
{
    var item = repo.GetById(id);
    return item;
}

However if that item is not found it will return null, rather than a 404.

So it could be better written as

[ResponseType(typeof(YourClass))]
public IHttpActionResult Get(int id)
{
    var item = repo.GetById(id);
    if (item != null)
    {
        return this.Ok(item);
    }

    return this.NotFound();
}

See more uses of this here http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing

See also customizing Swashbuckle and how it ResponseType attributes can be used to determine documentation https://azure.microsoft.com/en-gb/documentation/articles/app-service-api-dotnet-swashbuckle-customize/

like image 97
JConstantine Avatar answered Nov 28 '22 23:11

JConstantine