Created
January 30, 2014 03:52
-
-
Save bjcull/8702230 to your computer and use it in GitHub Desktop.
A Filter Attribute that lets you download an ASP.NET MVC View as a Word Document
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WordDocumentAttribute : ActionFilterAttribute | |
{ | |
public string DefaultFilename { get; set; } | |
public override void OnActionExecuted(ActionExecutedContext filterContext) | |
{ | |
var result = filterContext.Result as ViewResult; | |
if (result != null) | |
result.MasterName = "~/Views/Shared/_LayoutWord.cshtml"; | |
filterContext.Controller.ViewBag.WordDocumentMode = true; | |
base.OnActionExecuted(filterContext); | |
} | |
public override void OnResultExecuted(ResultExecutedContext filterContext) | |
{ | |
var filename = filterContext.Controller.ViewBag.WordDocumentFilename; | |
filename = filename ?? DefaultFilename ?? "Document"; | |
filterContext.HttpContext.Response.AppendHeader("Content-Disposition", string.Format("filename={0}.doc", filename)); | |
filterContext.HttpContext.Response.ContentType = "application/msword"; | |
base.OnResultExecuted(filterContext); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank useful code 💃