This project is read-only.
1
Vote

MediaTypeFormatter.WriteToStreamAsync can not set headers

description

I define a custom MediaTypeFormatter to handle downloading csv file.
The csv object model like
public class CsvModel: Collection<TRow> where TRow: class {
    // specific the download filename
    public string FileName {get;set;}
}
I want to specific the download filename in MediaTypeFormatter, but...
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
{
    // set content headers here was ignore
}
public override void SetDefaultContentHeaders(Type type, /*, object value*/ HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
{
    base.SetDefaultContentHeaders(type, headers, mediaType);
    headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        // set headers here works, but can not get the object value as in WriteToStreamAsync
        FileName = ((CsvModel)value).FileName;
    };
}
I think the SetDefaultContentHeaders method should pass the object value via parameters.

comments