Below code converts MVC view to word document but doesn't put extension, any suggestion how I can also get extension .doc added?

public ActionResult ToWord()
{
    var model = Logic.GetModel();
    string html = Utils.ViewToString(this, "MyView", model);
    return Content(html, "application/msword");
}

//Utils Class

static string ViewToString(Controller controller, string viewName, object model)
{
    controller.ViewData.Model = model;
    using (StringWriter sw = new StringWriter())
    {
        ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
        ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
        viewResult.View.Render(viewContext, sw);
        return sw.ToString();
    }
}

I have tried adding below code but it just ignores extension extension, saving document as file.doc then opens/displays word dcoument correctly

HttpContext.Response.AppendHeader("Content-Disposition", "wordFile.doc");

any ideas how I can get extension added to the file?

Related posts

Recent Viewed