Click here to Skip to main content
15,395,065 members
Articles / Programming Languages / C#
Technical Blog
Posted 22 Jan 2013

Tagged as


3 bookmarked

Setting a "Content-Disposition" HTTP Header in Web API Controller Method

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
22 Jan 2013CPOL1 min read
Setting a "Content-Disposition" HTTP Header in the Web API Controller method.

When developing an ASP.NET Web API controller method that allows to download a file it is considered a good practice to set a "Content-Disposition" header to guide a browser of a name of a file being downloaded.

There are two different ways to approach this simple task. First is to add a new HTTP header to the HTTP response using string values for the header's name and value. The second is to try and use the .NET Framework's infrastructure methods that presumably should make this task easier.

The first method seems to be very simple but potentially dangerous: one would need to hard-code string values and be totally responsible for the values to be properly formatted according to the W3C standards.

var response = new HttpResponseMessage();
response.Headers.Add("Content-Disposition", "attachment; filename=fileName.ext");

The second method is to try and delegate handling of the specifics of the HTTP protocol to the .NET Framework by using corresponding presumably built-in methods. This seems more appropriate and even easier to achieve. However due to poor documentation of the Web API extensions that is easier said than done. without further ado this is how it's done:

var response = new HttpResponseMessage();
response.Content.Headers.ContentDisposition = 
   new ContentDispositionHeaderValue("attachment") { FileName = fileName};

Only one string value here should hard-coded with a guideline available in the official documentation so it's not that bad.

If you find this information possible share it with your fellow colleagues so it may save their valuable time.

This article was originally posted at


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alexander Turlov
Canada Canada
Alexander Turlov is a professional software development consultant that has been working in IT industry since 1987. His programming experience includes such languages as FORTRAN, Pascal, Basic, C, C++ and C#. He's been working for different industries including but not limited to science, manufacturing, retail, utilities, finance, insurance, health care, education and so on. His area of professional interests is cloud powered rich web applications development with .NET, C#, ASP.NET/MVC and JavaScript. He is working in software development doing architecture, design and development on .NET platform and using Microsoft Visual Studio, Azure and Visual Studio Team Services as his primary tools. He holds a M.Sc. degree in physics and various industry certifications including MCSD.NET, Azure and Scrum.

View my profile on LinkedIn

View my blog

Comments and Discussions

QuestionHandling the response on the client side Pin
meatcp30-Jan-13 3:08
Membermeatcp30-Jan-13 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.