Content negotiation to return HTML

cvbarros

After reading this blog post on how to return HTML from Web API 2 using IHttpActionResult, I wanted to somehow "wire-up" this IHttpActionResult to my ApiController based on the Accept header that is sent with request.

Given controller actions that have signature similar to this:

public MyObject Get(int id)
{
    return new MyObject();
}

If the request specifies the Accept: text/html, this IHttpActionResult should be used to return HTML. Is that possible? In addition, some insight on how this content negotiation pipeline works for json or xml (that have built-in support) would be greatly appreciated.

Kiran Challa

If we keep the discussion of IHttpActionResult aside for a momment, Content-negotiation process in Web API is driven through formatters. So you would need to create a new formatter for handling the media type text/html.

Web API exposes the default algorithm it uses for content-negotiation called DefaultContentNegotiator which is an implementation of the service IContentNegotiator.

Now this negotiation algorithm can be run either by Web API automatically for you like in the following cases:

Usage # 1:

public MyObject Get(int id)
{
   return new MyObject();
}

OR

you can manually run the negotiation yourself like in the following:

Usage #2 :

public HttpResponseMessage Get()
{
    HttpResponseMessage response = new HttpResponseMessage();

    IContentNegotiator defaultNegotiator = this.Configuration.Services.GetContentNegotiator();
    ContentNegotiationResult negotationResult = defaultNegotiator.Negotiate(typeof(string), this.Request, this.Configuration.Formatters);

    response.Content = new ObjectContent<string>("Hello", negotationResult.Formatter, negotationResult.MediaType);
    return response;
}

Regarding IHttpActionResults:
In the following scenario, Ok<> is a shortcut method for generating an instance of type OkNegotiatedContentResult<>.

public IHttpActionResult Get()
{
    return Ok<string>("Hello");
}

The thing is this OkNegotiatedContentResult<> type does similar thing as in Usage # 2 scenario above. i.e they run the negotiator internally.

So to conclude, if you plan to support text/html media type then you need to write a custom formatter and add it to Web API's formatter collection and then when you use Ok<string>("Hello") with an Accept header of text/html, you should see the response in text/html. Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why not use content negotiation to return JSON object?

From Dev

PhalconPHP: content negotiation?

From Dev

Restlet Content Type Negotiation

From Dev

Content negotiation using WebSharper

From Dev

PhalconPHP: content negotiation?

From Dev

Spring boot controller content negotiation

From Dev

Jersey - content negotiation for error handling

From Dev

Spring MVC Content Negotiation throwing ClassCastException

From Dev

SupportedMediaTypes not used when doing content negotiation

From Dev

How to Use Content Negotiation in Spring Data Rest?

From Dev

How to disable content negotiation for Spring Actuators?

From Dev

Content Negotiation when looking at different output formats

From Dev

Content negotiation for File Download In Web API

From Dev

Is there a simple way to solve content negotiation in Spring boot

From Dev

How to Use Content Negotiation in Spring Data Rest?

From Dev

Content negotiation not working with respond_to stanza

From Dev

What is performing Transparent Content Negotiation in Apache

From Dev

Tastypie: return html content in API response

From Dev

How to return html content without escaping in serializer?

From Dev

Does HTTP content negotiation respect media type parameters

From Dev

spring cloud config-server .properties content-negotiation failing

From Dev

Is there better approach to Accept Header based content negotiation in Spring API?

From Dev

Why would HTTP content negotiation be preferred to explicit parameters in an API scenario?

From Dev

Is HTTP content negotiation being used by browsers and servers in practice?

From Dev

How to add csv MimeType to spring content-negotiation?

From Dev

Loop through HTML content and return values as global variables

From Dev

Wikipedia API doesn't return the same content that is in HTML

From Dev

Is there a way for an AJAX call to return both HTML content and callback functions?

From Dev

Return an Excel file from Java Servlet along with HTML content

Related Related

  1. 1

    Why not use content negotiation to return JSON object?

  2. 2

    PhalconPHP: content negotiation?

  3. 3

    Restlet Content Type Negotiation

  4. 4

    Content negotiation using WebSharper

  5. 5

    PhalconPHP: content negotiation?

  6. 6

    Spring boot controller content negotiation

  7. 7

    Jersey - content negotiation for error handling

  8. 8

    Spring MVC Content Negotiation throwing ClassCastException

  9. 9

    SupportedMediaTypes not used when doing content negotiation

  10. 10

    How to Use Content Negotiation in Spring Data Rest?

  11. 11

    How to disable content negotiation for Spring Actuators?

  12. 12

    Content Negotiation when looking at different output formats

  13. 13

    Content negotiation for File Download In Web API

  14. 14

    Is there a simple way to solve content negotiation in Spring boot

  15. 15

    How to Use Content Negotiation in Spring Data Rest?

  16. 16

    Content negotiation not working with respond_to stanza

  17. 17

    What is performing Transparent Content Negotiation in Apache

  18. 18

    Tastypie: return html content in API response

  19. 19

    How to return html content without escaping in serializer?

  20. 20

    Does HTTP content negotiation respect media type parameters

  21. 21

    spring cloud config-server .properties content-negotiation failing

  22. 22

    Is there better approach to Accept Header based content negotiation in Spring API?

  23. 23

    Why would HTTP content negotiation be preferred to explicit parameters in an API scenario?

  24. 24

    Is HTTP content negotiation being used by browsers and servers in practice?

  25. 25

    How to add csv MimeType to spring content-negotiation?

  26. 26

    Loop through HTML content and return values as global variables

  27. 27

    Wikipedia API doesn't return the same content that is in HTML

  28. 28

    Is there a way for an AJAX call to return both HTML content and callback functions?

  29. 29

    Return an Excel file from Java Servlet along with HTML content

HotTag

Archive