MVC指定ビューHTMLコード構造の読み出し
2528 ワード
public static class ControllerExtensions
{
private static readonly Regex m_regex = new Regex(@"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[
])\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);
}
}
}