SupportedMediaTypes not used when doing content negotiation

Eric Lundmark

I have created a new formatter in order to handle content negotiation.

public TiffImageFormatter()
{
    SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/tiff"));
}

public override bool CanReadType(Type type)
{
    return type == typeof(byte[]);
}

public override bool CanWriteType(Type type)
{
    return type == typeof(byte[]);
}

But when running

var negotiator = Configuration.Services.GetContentNegotiator();
var type = negotiator.Negotiate(typeof(byte[]), Request, Configuration.Formatters);

The supported media types are not taken into consideration instead "CanWritetype" is the only condition determining what accept type to use.

Accept: image/*, image/png, image/tiff, */* should result in image/tiff but Accept: image/png should return in null allowing me to send not acceptable.

How can I determine which is the correct media type?

Yuval Itzchakov

You need to use MediaTypeFormatter.MediaTypeMappings and add the relevant Accept headers:

public TiffImageFormatter()
{
    SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/tiff"));
    MediaTypeMappings.Add(
                new RequestHeaderMapping("Accept", "image\tiff", 
                                          StringComparison.OrdinalIgnoreCase,
                                          false, "image\tiff"));
}

This blog post explains the algorithm for media type matching if you need any further information.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Content Negotiation when looking at different output formats

From Dev

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

From Dev

Content negotiation to return HTML

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

Why not use content negotiation to return JSON object?

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 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 Java

What does "Filtering content" mean when doing a git clone?

From Dev

Algorithm Negotiation failed when trying to connect to server

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

How to add csv MimeType to spring content-negotiation?

From Dev

What fd's are used when doing diff <(cat old) <(cat new)?

From Dev

Can a boost::asio::yield_context be used as a deadline_timer handler when doing cancel?

From Dev

JApplet Content Clears When Thread.sleep Used

From Dev

Display content inside control when used negative margin.

Related Related

  1. 1

    Content Negotiation when looking at different output formats

  2. 2

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

  3. 3

    Content negotiation to return HTML

  4. 4

    PhalconPHP: content negotiation?

  5. 5

    Restlet Content Type Negotiation

  6. 6

    Content negotiation using WebSharper

  7. 7

    PhalconPHP: content negotiation?

  8. 8

    Spring boot controller content negotiation

  9. 9

    Jersey - content negotiation for error handling

  10. 10

    Spring MVC Content Negotiation throwing ClassCastException

  11. 11

    Why not use content negotiation to return JSON object?

  12. 12

    How to Use Content Negotiation in Spring Data Rest?

  13. 13

    How to disable content negotiation for Spring Actuators?

  14. 14

    Content negotiation for File Download In Web API

  15. 15

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

  16. 16

    How to Use Content Negotiation in Spring Data Rest?

  17. 17

    Content negotiation not working with respond_to stanza

  18. 18

    What is performing Transparent Content Negotiation in Apache

  19. 19

    What does "Filtering content" mean when doing a git clone?

  20. 20

    Algorithm Negotiation failed when trying to connect to server

  21. 21

    Does HTTP content negotiation respect media type parameters

  22. 22

    spring cloud config-server .properties content-negotiation failing

  23. 23

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

  24. 24

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

  25. 25

    How to add csv MimeType to spring content-negotiation?

  26. 26

    What fd's are used when doing diff <(cat old) <(cat new)?

  27. 27

    Can a boost::asio::yield_context be used as a deadline_timer handler when doing cancel?

  28. 28

    JApplet Content Clears When Thread.sleep Used

  29. 29

    Display content inside control when used negative margin.

HotTag

Archive