public static class ControllerExtensions
{
private static readonly Regex m_regex = new Regex(@"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}");
public static string RenderView(this Controller controller, string viewName, object model, string master = null)
{
var vd = new ViewDataDictionary();
foreach (var item in controller.ViewData.Keys)
{
vd.Add(item, controller.ViewData[item]);
}
vd.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, master);
ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, vd, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
return m_regex.Replace(sw.GetStringBuilder().ToString(), string.Empty);
}
}
}
转载于:https://my.oschina.net/u/926126/blog/302382