Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8439375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:59:45+00:00 2026-06-10T07:59:45+00:00

If a request is made to my Web API service that has a Content-Type

  • 0

If a request is made to my Web API service that has a Content-Type header containing a type not supported by that service, it returns a 500 Internal Server Error status code with a message similar to the following:

{"Message":"An error has occurred.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'MyDto' from content with media type 'application/UnsupportedContentType'.","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
   at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
   at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
   at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator, CancellationToken cancellationToken)"}

I would instead prefer to return a 415 Unsupported Media Type status code as recommended, e.g, here.

How do I configure my service to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:59:46+00:00Added an answer on June 10, 2026 at 7:59 am

    Here is the solution I came up with to this problem.

    It’s broadly based on the one described here for sending a 406 Not Acceptable status code when there’s no acceptable response content type.

    public class UnsupportedMediaTypeConnegHandler : DelegatingHandler {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                               CancellationToken cancellationToken) {
            var contentType = request.Content.Headers.ContentType;
            var formatters = request.GetConfiguration().Formatters;
            var hasFormetterForContentType = formatters //
                .Any(formatter => formatter.SupportedMediaTypes.Contains(contentType));
    
            if (!hasFormetterForContentType) {
                return Task<HttpResponseMessage>.Factory //
                    .StartNew(() => new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType));
            }
    
            return base.SendAsync(request, cancellationToken);
        }
    }
    

    And when setting up your service config:

    config.MessageHandlers.Add(new UnsupportedMediaTypeConnegHandler());
    

    Note that this requires that that the char sets match as well. You could loosen this restriction by checking only the MediaType property of the header.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm planning an XML web service that takes an XML request and returns an
Consider an ASP.NET Web API service that redirects public class ThisController : ApiController {
I'm working on a Python library that interfaces with a web service API. Like
How do I capture HTTP response when the request is made by web browser?
A simple get request made to url on my server which returns HTML of
I'm contemplating writing a web application with Rails. Each request made by the user
When a request is made to a web page, and it is handled via
Looking to develop a web service (api) in PHP to offer customers an easier
I'm trying to make my first web service client for the eBay API using
How can I set the HTTP_REFERER request header for a request made from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.