zoukankan      html  css  js  c++  java
  • 格式化asp.net mvc视图页面

    public static class ContollerExtensions
        {
            public static ViewFormatResult ViewFormat(this Controller controller)
            {
                return ViewFormat(controller,null /* viewName */, null /* masterName */, null /* model */);
            }
    
            public static ViewFormatResult ViewFormat(this Controller controller,object model)
            {
                return ViewFormat(controller, null /* viewName */, null /* masterName */, model);
            }
    
            public static ViewFormatResult ViewFormat(this Controller controller, string viewName)
            {
                return ViewFormat(controller, viewName, null /* masterName */, null /* model */);
            }
    
            public static ViewFormatResult ViewFormat(this Controller controller, string viewName, string masterName, object model)
            {
                if (model != null)
                {
                    controller.ViewData.Model = model;
                }
                return new ViewFormatResult
                {
                    ViewName = viewName,
                    MasterName = masterName,
                    ViewData = controller.ViewData,
                    TempData = controller.TempData
                };
            }
    View Code
    public class ViewFormatResult : ViewFormatResultBase
        {
            private string _masterName;
    
            public string MasterName
            {
                get
                {
                    return _masterName ?? String.Empty;
                }
                set
                {
                    _masterName = value;
                }
            }
    
            protected override ViewEngineResult FindView(ControllerContext context)
            {
                ViewEngineResult result = ViewEngineCollection.FindView(context, ViewName, MasterName);
                if (result.View != null)
                {
                    return result;
                }
    
                // we need to generate an exception containing all the locations we searched
                StringBuilder locationsText = new StringBuilder();
                foreach (string location in result.SearchedLocations)
                {
                    locationsText.AppendLine();
                    locationsText.Append(location);
                }
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                    "{0}{1}", ViewName, locationsText));
            }
    View Code
    public abstract class ViewFormatResultBase : ViewResultBase
        {
            public override void ExecuteResult(ControllerContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }
                if (String.IsNullOrEmpty(ViewName))
                {
                    ViewName = context.RouteData.GetRequiredString("action");
                }
    
                ViewEngineResult result = null;
    
                if (View == null)
                {
                    result = FindView(context);
                    View = result.View;
                }
    
                TextWriter writer = context.HttpContext.Response.Output;
    
                ViewContext viewContext = new ViewContext(context, View, ViewData, TempData, writer);
    
                WriterHtml(writer, viewContext);
    
                //View.Render(viewContext, writer);
    
                if (result != null)
                {
                    result.ViewEngine.ReleaseView(context, View);
                }
            }
    
            public void WriterHtml(TextWriter writer, ViewContext viewContext)
            {
                StringWriter sw = new StringWriter();
                HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
                View.Render(viewContext,htmlWriter);
                string html = sw.ToString();
                html = Regex.Replace(html, "[f
    
    	v]", "");
                html = Regex.Replace(html, " {2,}", " ");
                html = Regex.Replace(html, ">[ ]{1}", ">");
                string pattern = @"//<![CDATA[";
                html = Regex.Replace(html, pattern, "//<![CDATA[
    ");
                html = Regex.Replace(html, @"</head>", "</head>
    ");
                html = Regex.Replace(html, @"</body>", "</body>
    ");
    
                writer.Write(html);
            }
        }
    View Code
    最后调用
    public ActionResult Index()
            {
                return this.ViewFormat();
            }
  • 相关阅读:
    window+Apache+php+mysql注意事项
    【转】QRCode二维码的应用心得
    如何创建和配置服务器证书进行SSL Relay
    Citrix 实践中的问题及解决
    web.config中httpRunTime的属性
    [原]IE9 DOM的自定义属性问题
    添加图片到数据库
    Windows server 2008安装企业CA证书服务
    EXTJS Date 的转换格式化字符
    Extjs 4.1 学习笔记(一)(proxy,loader,treestore)
  • 原文地址:https://www.cnblogs.com/skys-net/p/4877218.html
Copyright © 2011-2022 走看看