ASP.NET MVC2 in Action 读书笔记 [9-2] CustomActionResults
Export.aspx:
<h2>Export</h2>
<a href="<%=Url.Action("ExportUsers") %>">Export Users as CSV</a>
HomeController.cs:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Export()
{
return View();
}
public ActionResult ExportUsers()
{
IEnumerable<User> model = UserRepository.GetUsers();
return new CsvActionResult(model);
}
public ActionResult Logout()
{
return new LogoutActionResult(RedirectToAction("Index","Home"));
}
}
CsvActionResult.cs:
public class CsvActionResult : ActionResult
{
public IEnumerable ModelListing { get; set; }
public CsvActionResult(IEnumerable modelListing)
{
ModelListing = modelListing;
}
public override void ExecuteResult(ControllerContext context)
{
byte[] data = new CsvFileCreator().AsBytes(ModelListing);
var fileResult = new FileContentResult(data, "text/csv")
{
FileDownloadName = "CsvFile.csv"
};
fileResult.ExecuteResult(context);
}
}
CsvFileCreator.cs:
public class CsvFileCreator
{
public byte[] AsBytes(IEnumerable modelList)
{
StringBuilder sb = new StringBuilder();
BuildHeaders(modelList, sb);
BuildRows(modelList, sb);
return sb.AsBytes();
}
private void BuildRows(IEnumerable modelList, StringBuilder sb)
{
foreach (object modelItem in modelList)
{
BuildRowData(modelList, modelItem, sb);
sb.NewLine();
}
}
private void BuildRowData(IEnumerable modelList, object modelItem, StringBuilder sb)
{
foreach (PropertyInfo info in modelList.GetType().GetElementType().GetProperties())
{
object value = info.GetValue(modelItem, new object[0]);
sb.AppendFormat("{0},", value);
}
}
private void BuildHeaders(IEnumerable modelList, StringBuilder sb)
{
foreach (PropertyInfo property in modelList.GetType().GetElementType().GetProperties())
{
sb.AppendFormat("{0},",property.Name);
}
sb.NewLine();
}
}
StringBuilderExtensions.cs:
public static class StringBuilderExtensions
{
public static void NewLine(this StringBuilder stringBuilder)
{
stringBuilder.Remove(stringBuilder.Length - 1, 1);
stringBuilder.Append(Environment.NewLine);
}
public static byte[] AsBytes(this StringBuilder stringBuilder)
{
return stringBuilder.ToString().Select(c => Convert.ToByte((char) c)).ToArray();
}
}
UserRepository.cs:
public static class UserRepository
{
public static IEnumerable<User> GetUsers()
{
return new[]
{
new User
{
FirstName = "Fred",
LastName = "Flintstone",
LastLogin = new DateTime(2010, 3, 22),
Username = "FFlintstone"
},
new User
{
FirstName = "Willma",
LastName = "Flintstone",
LastLogin = new DateTime(2010, 1, 1),
Username = "WFlintstone"
},
new User
{
FirstName = "Jack",
LastName = "Flintstone",
LastLogin = new DateTime(2010, 2, 2),
Username = "JFlintstone"
},
new User
{
FirstName = "Jane",
LastName = "Flintstone",
LastLogin = new DateTime(2010, 3, 3),
Username = "JaFlintstone"
},
};
}
}
User.cs:
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public DateTime LastLogin { get; set; }
}
LogoutActionResult.cs:
public class LogoutActionResult : ActionResult
{
public RedirectToRouteResult ActionAfterLogout { get; set; }
public LogoutActionResult(RedirectToRouteResult actionAfterLogout)
{
ActionAfterLogout = actionAfterLogout;
}
public override void ExecuteResult(ControllerContext context)
{
FormsAuthentication.SignOut();
ActionAfterLogout.ExecuteResult(context);
}
}