随笔 - 25  文章 - 0 评论 - 10 阅读 - 20101
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

复制代码
1.输入框过滤或替换恶意字符
        
/// <summary>
        
/// Method to make sure that user's inputs are not malicious
        
/// </summary>
        
/// <param name="text">User's Input</param>
        
/// <param name="maxLength">Maximum length of input</param>
        
/// <returns>The cleaned up version of the input</returns>
        public static string InputText(string text, int maxLength) {
            text 
= text.Trim();
            
if (string.IsNullOrEmpty(text))
                
return string.Empty;
            
if (text.Length > maxLength)
                text 
= text.Substring(0, maxLength);
            text 
= Regex.Replace(text, "[\\s]{2,}"" ");    //two or more spaces
            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)""\n");    //<br>
            text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+"" ");    //&nbsp;
            text = Regex.Replace(text, "<(.|\\n)*?>"string.Empty);    //any other tags
            text = text.Replace("'""''");
            
return text;
        }
复制代码

 

复制代码
2. 缓存设置,缓存时间和是否开启缓存配置到配置文件中方便我们关闭和启用缓存
        
private static readonly int productTimeout = int.Parse(ConfigurationManager.AppSettings["ProductCacheDuration"]);
        
private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);

        
/// <summary>
        
/// This method acts as a proxy between the web and business components to check whether the 
        
/// underlying data has already been cached.
        
/// </summary>
        
/// <param name="category">Category</param>
        
/// <returns>List of ProductInfo from Cache or Business component</returns>
        public static IList<ProductInfo> GetProductsByCategory(string category) {

            Product product 
= new Product();

            
if (!enableCaching)
                
return product.GetProductsByCategory(category);

            
string key = "product_by_category_" + category;
            IList
<ProductInfo> data = (IList<ProductInfo>)HttpRuntime.Cache[key];

            
// Check if the data exists in the data cache
            if (data == null) {

                
// If the data is not in the cache then fetch the data from the business logic tier
                data = product.GetProductsByCategory(category);

                
// Create a AggregateCacheDependency object from the factory
                AggregateCacheDependency cd = DependencyFacade.GetProductDependency();

                
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object
                HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(productTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }

            
return data;
        }
复制代码

 

 

posted on   〤‵坠落者...  阅读(423)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示