MetaGuru
MetaGuru

Reputation: 43873

Is HttpResponse an acceptable return type for a public ASP.NET MVC Action?

I want to craft a Response and add file attachment headers, is this the way to go about doing this in ASP.NET MVC Action? Normally I return things like int, string, or more commonly ActionResult. But can I return an HttpResponse directly back to their browser like this?

So if they go to /controller/action/ it would return that HTTP response to their browser.

Upvotes: 1

Views: 1336

Answers (1)

Parrots
Parrots

Reputation: 26902

I use the FilePathResult return type whenever I want to return a file for download to the user:

public FilePathResult DownloadFile() {
    FilePathResult file = new FilePathResult("c:\\test.txt", "text/plain");
    return file;
}

The first param is the path to the file on your local system (or network share), the second param is the mime-type you want reported to the browser. You can set file.FileDownloadName if you want to manually customize the file name the user will see when prompted to download it.

Edit: Found a decent overview of the different options (FilePathResult, FileStreamResult, FileContentResult) incase you want to read up on it a bit more (bottom third of the post).

Upvotes: 2

Related Questions