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

📄 querystringmodule.cs

📁 动易SiteFactory&#8482 网上商店系统1.0源代码
💻 CS
字号:
namespace PowerEasy.Web.HttpModule
{
    using PowerEasy.Common;
    using PowerEasy.Web.Configuration;
    using System;
    using System.Collections.Specialized;
    using System.Globalization;
    using System.Web;
    using System.Web.Configuration;

    public class QueryStringModule : IHttpModule
    {
        private static string m_ErrorMessage;
        private QueryStringsSection m_QueryStringsSection;

        private void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            HttpRequest request = application.Request;
            string str = request.AppRelativeCurrentExecutionFilePath.ToLower(CultureInfo.CurrentCulture);
            PageElement pageElement = this.m_QueryStringsSection.Page[str];
            if ((pageElement != null) && (!Validate(request.QueryString, pageElement) && pageElement.AbortOnError))
            {
                HttpContext.Current.Response.Write(m_ErrorMessage);
                HttpContext.Current.Response.End();
            }
        }

        private static bool CheckParamValue(ParamElement paramElement, string paramValue)
        {
            ParamType type = paramElement.Type;
            if (((type == ParamType.String) && (paramElement.Length > -1)) && (paramValue.Length > paramElement.Length))
            {
                return false;
            }
            if ((type == ParamType.Int) && !DataValidator.IsNumber(paramValue))
            {
                return false;
            }
            if ((type == ParamType.Bool) && !(((string.Equals(paramValue, "yes", StringComparison.OrdinalIgnoreCase) | string.Equals(paramValue, "no", StringComparison.OrdinalIgnoreCase)) | string.Equals(paramValue, "true", StringComparison.OrdinalIgnoreCase)) | string.Equals(paramValue, "false", StringComparison.OrdinalIgnoreCase)))
            {
                return false;
            }
            return true;
        }

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            context.BeginRequest += new EventHandler(this.Application_BeginRequest);
            this.m_QueryStringsSection = (QueryStringsSection) WebConfigurationManager.GetSection("powereasy.web/queryStrings");
        }

        private static bool Validate(NameValueCollection postedQueryString, PageElement pageElement)
        {
            if (pageElement != null)
            {
                if (postedQueryString.Count > pageElement.Param.Count)
                {
                    m_ErrorMessage = "<li>参数太多!</li>";
                    return false;
                }
                for (int i = 0; i <= (postedQueryString.Count - 1); i++)
                {
                    string str = postedQueryString.Keys[i];
                    if (!string.IsNullOrEmpty(str))
                    {
                        str = str.ToLower(CultureInfo.CurrentCulture);
                    }
                    if (!pageElement.Param.Contains(str))
                    {
                        m_ErrorMessage = "<li>参数错误!</li>";
                        return false;
                    }
                }
                for (int j = 0; j <= (pageElement.Param.Count - 1); j++)
                {
                    ParamElement paramElement = pageElement.Param[j];
                    string paramValue = postedQueryString[paramElement.Name];
                    if (paramValue == null)
                    {
                        if (!paramElement.Optional)
                        {
                            m_ErrorMessage = "<li>缺少参数!</li>";
                            return false;
                        }
                    }
                    else if (!CheckParamValue(paramElement, paramValue))
                    {
                        m_ErrorMessage = "<li>参数值不符!</li>";
                        return false;
                    }
                }
            }
            return true;
        }

        public string ModuleName
        {
            get
            {
                return "QueryStringModule";
            }
        }
    }
}

⌨️ 快捷键说明

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