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 9094645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:23:55+00:00 2026-06-16T23:23:55+00:00

I have created an ASP.NET web API controller that is returning a strongly typed

  • 0

I have created an ASP.NET web API controller that is returning a strongly typed object on an action, as follows:

// GET api/iosdevices/5
public iOSDevice Get(string id) {
  return new iOSDevice();
}

I have created a BufferedMediaTypeFormatter to handle the type iOSDevice:

public class iOSDeviceXmlFormatter : BufferedMediaTypeFormatter
{
    public iOSDeviceXmlFormatter() {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
    }

    public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content) {
        content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
        iOSDevice device = (iOSDevice)value;
        using (XmlWriter writer = XmlWriter.Create(writeStream)) {
            writer.WriteStartElement("iOSDevice");
            if (device.identifierForVendor != Guid.Empty) {
                writer.WriteElementString("identifierForVendor", device.identifierForVendor.ToString());
                writer.WriteElementString("userInterfaceIdiom", device.userInterfaceIdiom);
                writer.WriteElementString("systemName", device.systemName);
                writer.WriteElementString("systemVersion", device.systemVersion);
                writer.WriteElementString("model", device.model);
            }
            writer.WriteEndElement();
        }
        writeStream.Close();
    }
}

My problem is when I catch type “text/html” (e.g. someone attempts to access the API from his or her web browser), the response type is “text/html” instead of “application/xml”. I want to override the response type so that the user gets a response that is “application/xml” instead of “text/html”.

I cannot in the ApiController type get access to the “Response” property that is on regular MVC controllers and I am at a loss. How do I override the response type for this action that is being handled by a media type formatter?

EDIT: HELPFUL NOTE

I was trying this previously:

var response = Request.CreateResponse<iOSDevice>(HttpStatusCode.Accepted, device);
response.Headers.Remove("Content-Type");
response.Headers.Add("Content-Type", "application/xml; charset=utf-8");
return response;

And it claimed I was “misusing” the headers.

But when I used Filip’s example below of setting Content directly, it worked!

var response = Request.CreateResponse();
response.Content = new ObjectContent<iOSDevice>(device, new iOSDeviceXmlFormatter());
return response;
  • 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-16T23:23:57+00:00Added an answer on June 16, 2026 at 11:23 pm

    When you write to stream in the formatter, headers have been already sent.

    You can do this:

        public HttpResponseMessage Get(string id) {
        {
            var value = new iOSDevice();
            var response = Request.CreateResponse();
            response.Content = new ObjectContent(typeof(iOSDevice), value, new iOSDeviceXmlFormatter());
            //set headers on the "response"
            return response;
        }
    

    or you can do this (add this method to your formatter):

        public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, string mediaType)
        {
            base.SetDefaultContentHeaders(type, headers, mediaType);
            headers.ContentType = new MediaTypeHeaderValue("application/xml");
        }
    

    Here is an example on how I used the SetDefaultContentHeaders with a custom formatter:
    http://www.strathweb.com/2012/09/generate-kindle-mobi-ebooks-with-your-asp-net-web-api/

       public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
       {
          if (CanWriteType(type) && mediaType.MediaType == supportedMediaType)
          {
             headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
             headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
             headers.ContentDisposition.FileName = "ebook.mobi";
          }
          else
          {
             base.SetDefaultContentHeaders(type, headers, mediaType);
          }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a ASP.NET Web API controller that uses a service, which in
I created an ASP.NET MVC4 Web API service (REST) with a single GET action.
I have created a Visual Studio 2010 ASP.NET Web Application. I've noticed that if
I have created a web page (ASP.NET) that includes a stylesheet to mimic Dynamics
I have created the hello world of ASP.NET MVC web API projects using VS2012:
I have an ASP.NET Web API server, that have to communicate with different applications
I have an existing web application that was created using ASP.NET C#, MVC3 with
I am trying to make a secure asp.net web api. For that I have
I have a RESTful API created with ASP.NET MVC2 that returns all data as
I have created a filter which inherits the System.Web.Http.Filters.ActionFilterAttribute in the asp.net web api

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.