common.cs

来自「具有一般blog的相册、文章、作品等功能程序结构也比较清晰采用三层结构开发(利用」· CS 代码 · 共 66 行

CS
66
字号
using System;
using System.Web;
using System.Text.RegularExpressions;

namespace liuwei.FrameWork
{
	/// <summary>
	/// 摘要说明。
	/// </summary>
	public class Common
	{
		public Common()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}
		public static string GetUserIpAddress(HttpContext context)
		{
			string result = String.Empty;
			if (context == null) return result;

			result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
			if (null == result || result == String.Empty)
				result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

			return result;
		}

		public static string CheckForUrl(string text)
		{
			
			if(text == null || text.Trim().Length == 0 || text.Trim().ToLower().StartsWith("http://"))
			{
				return text;
			}
			return "http://" + text;
		}

		public static string SafeFormat(string stringToTransform) 
		{
			stringToTransform = HttpContext.Current.Server.HtmlEncode(stringToTransform);

			return stringToTransform.Replace("\n", "<br>");
		}

		public static string SafeFormatWithUrl(string stringToTransform)
		{
			return EnableUrls(SafeFormat(stringToTransform));
		}

		public static string EnableUrls(string text)
		{
			string pattern = @"(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])";
			MatchCollection matchs;
                        
			matchs = Regex.Matches(text,pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
			foreach (Match m in matchs) 
			{
				text = text.Replace(m.ToString(), "<a target=\"_new\" href=\"" + m.ToString() + "\">" + m.ToString() + "</a>");
			}
			return text;			
		}
	}
}

⌨️ 快捷键说明

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