class WebUtility
这个类代码
1
using System;2
using System.Text.RegularExpressions;3
using System.Web;4
using PetShop.BLL;5
using System.Web.Caching;6
using PetShop.CacheDependencyFactory;7
using System.Configuration;8

9

namespace PetShop.Web
{10

/**//// <summary>11
/// Collection of utility methods for web tier12
/// </summary>13

public static class WebUtility
{14

15
private const string REDIRECT_URL = "~/Search.aspx?keywords={0}";16
private const string CATEGORY_NAME_KEY = "category_name_{0}";17
private const string PRODUCT_NAME_KEY = "product_name_{0}";18
private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);19

20

21

/**//// <summary>22
/// Method to make sure that user's inputs are not malicious23
/// </summary>24
/// <param name="text">User's Input</param>25
/// <param name="maxLength">Maximum length of input</param>26
/// <returns>The cleaned up version of the input</returns>27

public static string InputText(string text, int maxLength)
{28
text = text.Trim();29
if (string.IsNullOrEmpty(text))30
return string.Empty;31
if (text.Length > maxLength)32
text = text.Substring(0, maxLength);33
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces34
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>35
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // 36
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags37
text = text.Replace("'", "''");38
return text;39
}40

41

/**//// <summary>42
/// Method to check whether input has other characters than numbers43
/// </summary>44

public static string CleanNonWord(string text)
{45
return Regex.Replace(text, "\\W", "");46
}47

48

/**//// <summary>49
/// Method to redirect user to search page50
/// </summary>51
/// <param name="key">Search keyword</param> 52

public static void SearchRedirect(string key)
{53
HttpContext.Current.Response.Redirect(string.Format(REDIRECT_URL, InputText(key, 255)));54
}55

56

/**//// <summary>57
/// Method to retrieve and cache category name by its ID58
/// </summary>59
/// <param name="categoryId">Category id</param>60
/// <returns>Category name</returns>61

public static string GetCategoryName(string categoryId)
{62

63
Category category = new Category();64
if (!enableCaching)65
return category.GetCategory(categoryId).Name;66

67
string cacheKey = string.Format(CATEGORY_NAME_KEY, categoryId);68

69
// Check if the data exists in the data cache70
string data = (string)HttpRuntime.Cache[cacheKey];71

if (data == null)
{72
// Caching duration from Web.config73
int cacheDuration = int.Parse(ConfigurationManager.AppSettings["CategoryCacheDuration"]);74

75
// If the data is not in the cache then fetch the data from the business logic tier76
data = category.GetCategory(categoryId).Name;77

78
// Create a AggregateCacheDependency object from the factory79
AggregateCacheDependency cd = DependencyFacade.GetCategoryDependency();80

81
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object82
HttpRuntime.Cache.Add(cacheKey, data, cd, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);83
}84

85
return data;86
}87

88

/**//// <summary>89
/// Method to retrieve and cache product name by its ID90
/// </summary>91
/// <param name="productId">Product id</param>92
/// <returns>Product name</returns>93

public static string GetProductName(string productId)
{94

95
Product product = new Product();96
if (!enableCaching)97
return product.GetProduct(productId).Name;98

99
string cacheKey = string.Format(PRODUCT_NAME_KEY, productId);100

101
// Check if the data exists in the data cache102
string data = (string)HttpRuntime.Cache[cacheKey];103

104

if (data == null)
{105
// Caching duration from Web.config106
int cacheDuration = int.Parse(ConfigurationManager.AppSettings["ProductCacheDuration"]);107

108
// If the data is not in the cache then fetch the data from the business logic tier109
data = product.GetProduct(productId).Name;110

111
// Create a AggregateCacheDependency object from the factory112
AggregateCacheDependency cd = DependencyFacade.GetProductDependency();113

114
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object115
HttpRuntime.Cache.Add(cacheKey, data, cd, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);116

117
}118

119
return data;120
}121
}122
}
private const string REDIRECT_URL = "~/Search.aspx?keywords={0}";
private const string CATEGORY_NAME_KEY = "category_name_{0}";
private const string PRODUCT_NAME_KEY = "product_name_{0}";
private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);
public static string InputText(string text, int maxLength)
public static string CleanNonWord(string text)public static void SearchRedirect(string key) 
public static string GetCategoryName(string categoryId)
public static string GetProductName(string productId)一个工具类的样子