views:

853

answers:

2

I need to create an ActionResult in an ASP.NET MVC application which has a .csv filetype.

I will provide a 'do not call' email list to my marketing partners and i want it to have a .csv extension in the filetype. Then it'll automatically open in Excel.

 http://www.example.com/mailinglist/donotemaillist.csv?password=12334

I have successfully done this as follows, but I want to make sure this is the absolute best and recommended way of doing this.

    [ActionName("DoNotEmailList.csv")]
    public ContentResult DoNotEmailList(string username, string password)
    {
            return new ContentResult()
            {
                Content = Emails.Aggregate((a,b)=>a+Environment.NewLine + b), 
                ContentType = "text/csv"
            };
    }

This Actionmethod will respond to the above link just fine.

I'm just wondering if there is any likelihood of any unexpected conflict of having the file extension like this with any different version of IIS, any kind of ISAPI filter, or anything else I cant think of now.

I need to be 100% sure because I will be providing this to external partners and don't want to have to change my mind later. I really cant see any issues, but maybe theres something obscure - or another more "MVC" like way of doing this.

+3  A: 

This is how I'm doing something similar. I'm treating it as a download:

var disposition = String.Format(
  "attachment;filename=\"{0}.csv\"", this.Model.Name);
Response.AddHeader("content-disposition", disposition);

This should show up in the browser as a file download with the given filename.

I can't think of a reason why yours wouldn't work, though.

Talljoe
+4  A: 

I think your Response MUST contain "Content-Disposition" header in this case. Create custom ActionResult like this:

public class MyCsvResult : ActionResult {

    public string Content {
        get;
        set;
    }

    public Encoding ContentEncoding {
        get;
        set;
    }

    public string Name {
        get;
        set;
    }

    public override void ExecuteResult(ControllerContext context) {
        if (context == null) {
            throw new ArgumentNullException("context");
        }

        HttpResponseBase response = context.HttpContext.Response;

        response.ContentType = "text/csv";

        if (ContentEncoding != null) {
            response.ContentEncoding = ContentEncoding;
        }

        var fileName = "file.csv";

        if(!String.IsNullOrEmpty(Name)) {
            fileName = Name.Contains('.') ? Name : Name + ".csv";
        }

        response.Headers.Add("Content-Disposition",
            String.Format("attachment; filename={0}", fileName));

        if (Content != null) {
            response.Write(Content);
        }
    }
}

And use it in your Action instead of ContentResult:

return new MyCsvResult {
    Content = Emails.Aggregate((a,b) => a + Environment.NewLine + b)
    /* Optional
     * , ContentEncoding = ""
     * , Name = "DoNotEmailList.csv"
     */
};
eu-ge-ne
thanks! but can you clarify what you mean by MUST ? what i had does appear to work - although this way is much more preferable
Simon_Weaver
If your Action returns ".csv" file then your clients want to "open" it or "save" it - so you must provide "Content-Disposition" header (although it is optional, see http://www.ietf.org/rfc/rfc2183.txt). Without this header the process of opening your file may differ in various browsers/OSes/machines
eu-ge-ne
Is there an error in the code? Shouldn't Name be a string instead of encoding? Also the compiler is telling me that ActionResult doesn't have a Response property
ADB
Thanks, AD - `Name` property fixed. `ActionResult` does not have `Response` property, but `context.HttpContext` has one.
eu-ge-ne
I've found the bug (`Response.Headers.Add ...`) - fixed (should be `response.Headers.Add ...`) - thanks again AD.
eu-ge-ne
MVC2 ASP.NET 4.0: Use response.AppendHeader instead of response.Headers.Add.
mxmissile