Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use built-in content type negotiation and just get access to the decision

I wanted to take advantage of built-in content negotiator and just get access to decision what formatter is going to be used. I don't want to use Request.Headers.Accept and check for whether it is json or xml content type because there lot of things are involved in that decision. Is there a way I can check at controller level or override any class that tells me what formatter going to be used OR what request content type is?

thanks in advance.

like image 236
user1186065 Avatar asked Nov 23 '25 06:11

user1186065


1 Answers

You can run conneg manually:

var conneg = Configuration.Services.GetContentNegotiator();
        var connegResult = conneg.Negotiate(
            typeof(YOUR_TYPE), Request, Configuration.Formatters
        );

And use the output whichever way you want:

//the valid media type
var mediaType = connegResult.MediaType;
//do stuff

//the relevant formatter
var formatter = connegResult.Formatter;
//do stuff
like image 136
Filip W Avatar answered Nov 24 '25 22:11

Filip W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!