{"id":683,"date":"2020-03-11T08:01:25","date_gmt":"2020-03-11T08:01:25","guid":{"rendered":"http:\/\/bruceng.com\/dev\/?p=683"},"modified":"2020-03-11T08:01:25","modified_gmt":"2020-03-11T08:01:25","slug":"use-custom-content-type-header-in-webapi-or-mvc","status":"publish","type":"post","link":"https:\/\/bruceng.com\/dev\/?p=683","title":{"rendered":"Use custom content-type header in WebAPI or MVC."},"content":{"rendered":"\n<p>The content-type header of the response is affected by the accept header of the request. This is not very obvious at first.<\/p>\n\n\n\n<p>So when you register a custom media type to the JsonFormatter or XmlFormatter, the content-type header will not be changed depending on the type of the response object. Rather it matches the accept header in the request.<\/p>\n\n\n\n<p>If the accept header in the request is not accepted, then the services will default to application\/json or application\/xml.<\/p>\n\n\n\n<p>Example: let&#8217;s say you have a service model called Student, a custom media type &#8220;application\/mycompany.com+json&#8221;, and a web service endpoint that returns a student by ID.<\/p>\n\n\n\n<p>If you just use the browser to make a request to this endpoint, the content-type header in the response will likely be set to &#8220;application\/json&#8221; or &#8220;application\/xml&#8221; depending on your preferred setting and browser.<\/p>\n\n\n\n<p>However if you set the accept header in the request to &#8220;application\/mycompany.com+json&#8221;, the content-type type response header will be set to the same type.<\/p>\n\n\n\n<p>There is no mapping between the types and the service model from the backend side.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The content-type header of the response is affected by the accept header of the request. This is not very obvious at first. So when you register a custom media type to the JsonFormatter or XmlFormatter, the content-type header will not be changed depending on the type of the response object. Rather it matches the accept &hellip; <a href=\"https:\/\/bruceng.com\/dev\/?p=683\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Use custom content-type header in WebAPI or MVC.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[2,12],"class_list":["post-683","post","type-post","status-publish","format-standard","hentry","category-problems","tag-asp-net","tag-web-api"],"_links":{"self":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts\/683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=683"}],"version-history":[{"count":1,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts\/683\/revisions"}],"predecessor-version":[{"id":684,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts\/683\/revisions\/684"}],"wp:attachment":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}