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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:09:38+00:00 2026-06-11T09:09:38+00:00

I have successfully implemented a Csv Media Type Formatter in my ASP.Net Web API

  • 0

I have successfully implemented a Csv Media Type Formatter in my ASP.Net Web API project. I am able to get results back in Csv format. However the resulting filename is “parts” without an extension.

Ideally, I want to be able to set this filename in the controller, but being able to add the extension globally would be a minimum.

Below are the examples I have found

Override OnGetResponseHeaders – I don’t see that as an option in the current version.
http://forums.asp.net/t/1782973.aspx/1?Setting+response+and+content+headers+esp+ContentDisposition+inside+a+MediaTypeFormatter

According to that article this should work

public override IEnumerable<KeyValuePair<string, string>> OnGetResponseHeaders(Type objectType, string mediaType, HttpResponseMessage responseMessage)
{
       return new[] { new KeyValuePair<string, string>("Content-Disposition", "attachment; filename=testing.csv") };
}

However Visual Studio says “There is no suitable method for override” and won’t compile when I add that to my custom Csv Formatter.

Return HttpMessageResponse from controller – How to set downloading file name in ASP.NET Web API
However this appears to just be pushing existing server files, which would take the Csv serialization out of the mix. Below is an attempt to make this approach work:

public HttpResponseMessage Get(string id)
{
    var response = new HttpResponseMessage();

    if (id == "test")
    {
        var data = GetTestData();
        response.StatusCode = HttpStatusCode.OK;
        response.Content = new StreamContent(data);
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = "testorama.csv";
        return response;
    }

    return null;
}

The issue here is that new StreamContent() is expecting a stream – is there a way to get the current stream that Custom Csv Formatter created?

Bottom line, how can I set the filename for a resultset that is first serialized to Csv format?

Solution

Thanks Claudio – that got me going in the right direction. A couple changes from what you posted:

  1. I was deriving from BufferedMediaTypeFormatter for my custom Csv
    Formatter and to use SetDefaultContentHeaders I had to instead
    derive from MediaTypeFormatter.

  2. SetDefaultContentHeaders accepts the mediaType parameter with a type of
    MediaTypeHeaderValue and not string.

Here is the final code:

public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
{
    base.SetDefaultContentHeaders(type, headers, mediaType);
    headers.Add("Content-Disposition", "attachment; filename=testorama.csv");
}
  • 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-11T09:09:39+00:00Added an answer on June 11, 2026 at 9:09 am

    Override the method SetDefaultContentHeaders on your MediaTypeFormatter

    public override void SetDefaultContentHeaders(
        Type type, HttpContentHeaders headers, string mediaType)
    {
        base.SetDefaultContentHeaders(type, headers, mediaType);
        headers.Add("Content-Disposition", "attachment; filename=yourname.csv");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully implemented my GridView now, but, as always, the whole ASP.NET life
I have successfully implemented a network application in visual CLR project using boost.asio. but
I have successfully implemented interop beftween Win32 application and managed .Net dll as described
I have successfully implemented slider to my Cocos2D project using this resource . The
I have successfully implemented face detection part in my Face Recognition project.Now i have
I have successfully implemented a standard WCF service bound to MSMQ using the net.msmq
I have successfully implemented this method of using the Win32 API to set the
I have successfully implemented django-ajax-uploader into my project. But I do not know how
I have been reading about the responsive web pattern and I have successfully implemented
I have successfully implemented the new pageupdates/add API for a venue with just 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.