⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 communityglobals.cs

📁 一个ASP.NET下的中文内容管理和社区系统
💻 CS
📖 第 1 页 / 共 2 页
字号:
namespace ASPNET.StarterKit.Communities {

    using System;
    using System.IO;
    using System.Web;
    using System.Web.UI;
    using System.Web.Caching;
    using System.Configuration;
    using System.Text;
    using System.Collections.Specialized;
    using System.Text.RegularExpressions;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;



    //*********************************************************************
    //
    // CommunityGlobals Class
    //
    // This class contains common utility methods used throughout 
    // the Community Framework such as path manipulation and text
    // formatting methods.
    //
    //*********************************************************************

    public class CommunityGlobals {


        //*********************************************************************
        //
        // CommunityID Property
        //
        // Represents the ID of the current community. This property
        // is valid after the CommunitiesModule handles the 
        // BeginRequest event. 
        //
        //*********************************************************************
        
        public static int CommunityID {
            get {
                try { 
                    return ((CommunityInfo)HttpContext.Current.Items[ "CommunityInfo" ]).ID;
                } catch {
                    return 0;
                }
            }
        }

        //*********************************************************************
        //
        // CommunityName Property
        //
        // Represents the name of the current community. This property
        // is valid after the CommunitiesModule handles the 
        // BeginRequest event. 
        //
        //*********************************************************************
        
        public static string CommunityName {
            get { return ((CommunityInfo)HttpContext.Current.Items[ "CommunityInfo" ]).Name; }
        }



    // *********************************************************************
    //  Text Formatting Methods
    // *********************************************************************


        public static string FormatText(AllowHtml useHtml, int sectionID, string text) {
            // Return when null
            if (text == null)
                return String.Empty;

            
            // if no HTML allowed, HTML Encode 
            if (useHtml == AllowHtml.None)
                text = FormatPlainText(text);
            else
                text = EnsureSafeAnchors(text);            

            // Apply transformations
            text = ApplyTransformations(sectionID, text);

            return text;
        }


        
        //*********************************************************************
        //
        // FormatPlainText Method
        //
        // This method is used for formatting all text that SHOULD NOT
        // contain HTML. This method 
        // HtmlEncodes the text and adds line breaks. 
        //
        //*********************************************************************

        public static string FormatPlainText(string text) {
            // HTML encode the text
            text = HttpUtility.HtmlEncode(text);
            
            // Add line breaks 
            text = Regex.Replace(text,"\n\n","<p>");
            text = Regex.Replace(text,"\n","<br>");

            return text;
        }



        
        //*********************************************************************
        //
        // ApplyTransformations Method
        //
        // This method applies each transformation to a string
        // of text. The transformations are precalculated when
        // the sections are first loaded in the SectionUtility
        // class. 
        //
        //*********************************************************************

        public static string ApplyTransformations(int sectionID, string text)
        {
            // Get the section transformation
            SectionInfo _sectionInfo = SectionUtility.GetSectionInfo(sectionID);
            string[] splitTrans = Regex.Split(_sectionInfo.Transformations, "\n");
            
            for (int i=0;i<splitTrans.Length;i+=2) {
               try {
                text = Regex.Replace(text, splitTrans[i], splitTrans[i+1], RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline); 
               } catch {
                // we don't do anything in the catch because we
                // are just hiding errors in the transformations               
               }
            }
            return text;
        }



        private static string EnsureSafeAnchors(string stringToTransform) {
            MatchCollection matchs;
                        
            // Ensure we have safe anchors
            matchs = Regex.Matches(stringToTransform, "&lt;a.href=(&quot;)?(?<url>http://((.|\\n)*?))(&quot;)?&gt;(?<target>((.|\\n)*?))&lt;/a&gt;", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            foreach (Match m in matchs) {
                stringToTransform = stringToTransform.Replace(m.ToString(), "<a target=\"_new\" href=\"" + m.Groups["url"].ToString() + "\">" + m.Groups["target"].ToString() + "</a>");
            }

            return stringToTransform;
        }


        
        //*********************************************************************
        //
        // TruncateWithEllipsis Method
        //
        // This method shortens a string to a certain length and
        // adds ... when the string is longer than a specified
        // maximum length.
        //
        //*********************************************************************

        public static string TruncateWithEllipsis(string text, int length) {
            if (text.Length > length)
                text = text.Substring(0,length) + "...";
            return text;
        
        
        }

        
        //*********************************************************************
        //
        // Truncate Method
        //
        // This method chops off the end of a string when the
        // string is longer than a maximum length.
        //
        //*********************************************************************

        public static string Truncate(string text, int length) {
            if (text.Length > length)
                text = text.Substring(0,length);
            return text;
        }


        //*********************************************************************
        //
        // StripTags Method
        //
        // This method strips all tags from a string (good for removing all
        // HTML.
        //
        //*********************************************************************

        public static string StripTags(string text) {
            text = Regex.Replace(text, @"&nbsp;", "", RegexOptions.IgnoreCase);
            return Regex.Replace(text, @"<.+?>", "", RegexOptions.Singleline);
        }


    
    // *********************************************************************
    //  Global Validation Methods
    // *********************************************************************


        
        //*********************************************************************
        //
        // ValidateLength Method
        //
        // This method associates a client-side and server-side
        // function with a custom validator to check whether a
        // string of text is less than 300 characters.
        //
        //*********************************************************************

        public static void ValidateLength(CustomValidator validator, string scriptName) {
            StringBuilder builder = new StringBuilder();
            
            // Form the script that is to be registered at client side.
            builder.Append("<script language=JavaScript>\n");
            builder.AppendFormat("function {0}(s, e) {{\n", scriptName);
            builder.AppendFormat("if (e.Value.length > 300)\n");
            builder.Append("  e.IsValid = false;\n");
            builder.Append("else\n");
            builder.Append("  e.IsValid = true;\n");
            builder.Append("}</script>");
            
            if(!validator.Page.IsClientScriptBlockRegistered(scriptName))
             validator.Page.RegisterClientScriptBlock(scriptName, builder.ToString());
            
            // Associate client-side validation
            validator.ClientValidationFunction = scriptName;

            // Add server-side validation
            validator.ServerValidate += new ServerValidateEventHandler(ServerValidateLength); 
        }    
            
        
        //*********************************************************************
        //
        // ServerValidateLength Method
        //
        // Server-side validation method used with a custom validator
        // to check whether a string is less than a specified length.
        //
        //*********************************************************************

        public static void ServerValidateLength(Object s, ServerValidateEventArgs e) {
            if (e.Value.Length > 300)
                e.IsValid = false;
            else
                e.IsValid = true;
        }





    // *********************************************************************
    //  Path Manipulation Methods
    // *********************************************************************


        //*********************************************************************
        //
        // ResolveAbsoluteUrl Method
        //
        // This method translates a relative path into an
        // absolute path.
        //
        //*********************************************************************

        public static string ResolveAbsoluteUrl(string Url) {
            if (HttpContext.Current.Request.IsSecureConnection)
                return "https://" + PrimaryDomain + Url;
            else
                return "http://" + PrimaryDomain + Url;        
        }
        



        //*********************************************************************
        //
        // CacheKey Method
        //
        // Generates a community relative cache key so that 
        // cached data can be partitioned from different 
        // communities.
        //
        //*********************************************************************

        public static string CacheKey(string key) {
           return CommunityID.ToString() + key;
        }



        //*********************************************************************
        //
        // ResolveBase Method
        //
        // Adds the base application path to a relative path.
        //
        //*********************************************************************
 
        public static string ResolveBase(string relativePath) {
            return AppPath + "/" + relativePath;
        }

        

        //*********************************************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -