📄 utility.cs
字号:
namespace PowerEasy.Web
{
using PowerEasy.Common;
using System;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
internal sealed class Utility
{
private Utility()
{
}
public static bool AccessingPath(string accessingurl, string path)
{
bool flag = accessingurl.StartsWith(path, StringComparison.CurrentCultureIgnoreCase);
bool flag2 = accessingurl.EndsWith("aspx", StringComparison.CurrentCultureIgnoreCase);
bool flag3 = accessingurl.EndsWith("/");
if (!flag)
{
return false;
}
if (!flag2)
{
return flag3;
}
return true;
}
public static string AppendSecurityCode(string currenturl)
{
if (!string.IsNullOrEmpty(currenturl))
{
if (currenturl.IndexOf("?") < 0)
{
currenturl = currenturl + "?sid=" + GetSecurityCode(currenturl);
return currenturl;
}
if ((currenturl.IndexOf("&sid=") < 0) && (currenturl.IndexOf("?sid=") < 0))
{
currenturl = currenturl + "&sid=" + GetSecurityCode(currenturl);
}
}
return currenturl;
}
public static string CombineRawUrl(string url)
{
if ((url[0] != '~') && (url.IndexOf(':') < 0))
{
string rawUrl = HttpContext.Current.Request.RawUrl;
if (rawUrl.IndexOf('?') > 0)
{
rawUrl = rawUrl.Split(new char[] { '?' })[0];
}
if (url.IndexOf('?') > 0)
{
string[] strArray = url.Split(new char[] { '?' });
url = VirtualPathUtility.Combine(rawUrl, strArray[0]) + "?" + strArray[1];
return url;
}
url = VirtualPathUtility.Combine(rawUrl, url);
}
return url;
}
public static string EnumToHtml<T>(T enumValue) where T: struct
{
string str = enumValue.ToString();
string name = enumValue.GetType().Name;
if (!str.Contains(","))
{
return GetGlobalEnumString(name + "_" + str);
}
StringBuilder builder = new StringBuilder();
foreach (string str3 in str.Split(new string[] { "," }, StringSplitOptions.None))
{
string resourceKey = name + "_" + str3.Trim();
if (builder.Length > 0)
{
builder.Append(",");
}
builder.Append(GetGlobalEnumString(resourceKey));
}
return builder.ToString();
}
public static string GetBasePath(HttpRequest request)
{
if (request == null)
{
return "/";
}
return VirtualPathUtility.AppendTrailingSlash(request.ApplicationPath);
}
public static string GetGlobalCacheString(string resourceKey)
{
return GetGlobalString("CacheResources", resourceKey);
}
public static string GetGlobalEnumString(string resourceKey)
{
return GetGlobalString("EnumResources", resourceKey);
}
public static string GetGlobalErrorString(string resourceKey)
{
return GetGlobalString("ErrorMessage", resourceKey);
}
public static string GetGlobalString(string classKey, string resourceKey)
{
string str = (string) HttpContext.GetGlobalResourceObject(classKey, resourceKey, CultureInfo.CurrentCulture);
if (str == null)
{
str = string.Empty;
}
return str;
}
public static string GetSecurityCode(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return string.Empty;
}
string str = "";
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
int index = filePath.IndexOf("?");
if (index > 0)
{
str = HttpUtility.ParseQueryString(filePath.Substring(index, filePath.Length - index))["Action"];
}
string s = fileNameWithoutExtension.ToLower(CultureInfo.CurrentCulture) + str.ToLower(CultureInfo.CurrentCulture) + HttpContext.Current.Session.SessionID;
HMACSHA1 hmacsha = new HMACSHA1(Encoding.UTF8.GetBytes(s));
return BitConverter.ToString(hmacsha.ComputeHash(Encoding.UTF8.GetBytes(s))).Replace("-", "").ToLower(CultureInfo.CurrentCulture);
}
public static int GetSelectedIndexByValue(ListControl listControl, string selectValue)
{
int index = -1;
if (listControl != null)
{
index = listControl.Items.IndexOf(listControl.Items.FindByValue(selectValue));
}
return index;
}
public static string RebuildPageName(string filename, NameValueCollection query)
{
if (string.IsNullOrEmpty(filename))
{
return string.Empty;
}
string[] strArray = filename.Split(new char[] { '/' });
if (strArray.Length > 0)
{
filename = strArray[strArray.Length - 1];
}
if (filename.IndexOf('?') > 0)
{
filename = filename.Substring(0, filename.IndexOf('?') - 1);
}
StringBuilder builder = new StringBuilder(filename);
if (query.Count > 0)
{
bool flag = false;
for (int i = 0; i < query.Count; i++)
{
if (i == 0)
{
builder.Append("?");
}
else
{
builder.Append("&");
}
if (query.GetKey(i) == "page")
{
builder.Append("page={$pageid/}");
flag = true;
}
else
{
builder.Append(query.GetKey(i) + "=" + DataSecurity.FilterBadChar(query.Get(i)));
}
}
if (!flag)
{
if (builder.Length > filename.Length)
{
builder.Append("&page={$pageid/}");
}
else
{
builder.Append("?page={$pageid/}");
}
}
}
else
{
builder.Append("?page={$pageid/}");
}
return builder.ToString();
}
public static int RequestInt32(string queryItem, int defaultValue)
{
return DataConverter.CLng(HttpContext.Current.Request.QueryString[queryItem], defaultValue);
}
public static string RequestString(string queryItem, string defaultValue)
{
string str = HttpContext.Current.Request.QueryString[queryItem];
if (str == null)
{
return defaultValue;
}
return str.Trim();
}
public static string RequestStringToLower(string queryItem, string defaultValue)
{
string str = HttpContext.Current.Request.QueryString[queryItem];
if (str == null)
{
return defaultValue.ToLower(CultureInfo.CurrentCulture);
}
return str.Trim().ToLower(CultureInfo.CurrentCulture);
}
public static void ResponseFileNotFound()
{
HttpContext.Current.Server.Transfer("NonexistentPage.aspx");
HttpContext.Current.Response.End();
}
public static void ResponseRedirect(string redirecturl, bool endResponse)
{
redirecturl = CombineRawUrl(redirecturl);
HttpContext.Current.Response.Redirect(redirecturl, endResponse);
}
public static void SetSelectedIndexByValue(ListControl listControl, string selectValue)
{
if (listControl != null)
{
listControl.SelectedIndex = listControl.Items.IndexOf(listControl.Items.FindByValue(selectValue));
}
}
public static void WriteErrMsg(string errorMessage, string returnurl)
{
HttpContext.Current.Items["ErrorMessage"] = errorMessage;
HttpContext.Current.Items["ReturnUrl"] = returnurl;
HttpContext.Current.Server.Transfer("~/Admin/Prompt/ShowError.aspx");
}
public static void WriteMessage(string message, string returnurl, string messageTitle)
{
HttpContext.Current.Items["Message"] = message;
HttpContext.Current.Items["ReturnUrl"] = returnurl;
HttpContext.Current.Items["MessageTitle"] = messageTitle;
HttpContext.Current.Server.Transfer("~/Admin/Prompt/ShowMessage.aspx");
}
public static void WriteSuccessMsg(string successMessage, string returnurl)
{
HttpContext.Current.Items["SuccessMessage"] = successMessage;
HttpContext.Current.Items["ReturnUrl"] = returnurl;
HttpContext.Current.Server.Transfer("~/Admin/Prompt/ShowSuccess.aspx");
}
public static void WriteUserErrMsg(string errorMessage, string returnurl)
{
HttpContext.Current.Items["ErrorMessage"] = errorMessage;
HttpContext.Current.Items["ReturnUrl"] = returnurl;
HttpContext.Current.Server.Transfer("~/Prompt/ShowError.aspx");
}
public static void WriteUserSuccessMsg(string successMessage, string returnurl)
{
HttpContext.Current.Items["SuccessMessage"] = successMessage;
HttpContext.Current.Items["ReturnUrl"] = returnurl;
HttpContext.Current.Server.Transfer("~/Prompt/ShowSuccess.aspx");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -