MVC 读取指定视图HTML 代码结构

 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);
            }
        }
    }
posted @ 2012-07-02 14:23  Rhythmk  阅读(1367)  评论(0编辑  收藏  举报
Rhythmk 个人笔记