Showing posts with label c# mvc2 aspose word. Show all posts
Showing posts with label c# mvc2 aspose word. Show all posts

Monday, March 21, 2011

AutoMapper for C#

I finally got around to using AutoMapper from fellow Austinite Jimmy Bogard. I watched Jimmy's helpful video and the rest was easy.
AutoMapper is a Data Transfer Object (DTO) helper framework that can map your domain objects to view objects.
In my case I wanted to add extra formatting to render my domain objects as HTML, but realized a "ToHtml()" method on the domain object doesn't smell so good. I created a View Model directly from the domain model via Automapper (with only the attributes I needed to display) and added the HTML rendering code there.
Is that the smell of a pine forest after a Spring rain?

Pine Forest by ®DS
Pine Forest a photo by ®DS on Flickr.

Wednesday, December 22, 2010

A way to download an Aspose Word document to a browser using MVC2

Perhaps there's a better way, but here is what I did quickly to get an Aspose Word document down to a browser. In the controller class use a MemoryStream to feed a FileStreamResult.
//Controller
public ActionResult MergeAndDownload(Choice choice) {
 MemoryStream memoryStream = new MemoryStream();
 Document document = ... // get your Aspose document
 document.Save(memoryStream, SaveFormat.Docx);
 memoryStream.Position = 0;
 var fileStreamResult = new FileStreamResult(memoryStream, "application/ms-word");
 fileStreamResult.FileDownloadName = "MySuggestedFilename.docx";
 return fileStreamResult;
}
In the View just do a Model.Save()
//View
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Aspose.Words.Document>" %>
<asp:Content ID="Content1" runat=server ContentPlaceHolderID="MainContent">
<%@ Import Namespace="Aspose.Words" %>
<% 
    Response.setContentType("application/msword"); 
    Model.Save(Response.OutputStream, SaveFormat.WordML); %>
</asp:Content>
If you know a simpler way, please post in comments.