Beispiel #1
0
 static RewriterConfiguration()
 {
     ExecuteExtension = ConfigurationManager.AppSettings["RewriteExtension"];
     if (string.IsNullOrEmpty(ExecuteExtension))
     {
         ExecuteExtension = ".htm";
     }
     instance = (RewriterConfiguration)ConfigurationManager.GetSection("RewriterConfig");
 }
Beispiel #2
0
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;

            if (IsUploadRequest(app.Context))
            {
                return;
            }

            #region Rewriter
            RewriterConfiguration config = RewriterConfiguration.GetConfig();
            if (config != null)
            {
                bool is404 = app.Request.Path.EndsWith(RewriterConfiguration.ExecuteExtension, StringComparison.OrdinalIgnoreCase);
                RewriterRuleCollection rules = config.Rules;
                for (int i = 0; i < rules.Count; i++)
                {
                    string requestedPath = null, lookFor = null;
                    if (rules[i].IsCrossdomain)
                    {
                        //二级域名重写
                        requestedPath = app.Request.Url.AbsoluteUri;
                        lookFor       = String.Concat("^", rules[i].LookFor, "$");
                    }
                    else
                    {
                        //一般重写
                        requestedPath = app.Request.Path;
                        lookFor       = String.Concat("^", RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor), "$");
                    }
                    Regex reg = new Regex(lookFor, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (reg.IsMatch(requestedPath))
                    {
                        is404 = false;
                        string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, reg.Replace(requestedPath, rules[i].SendTo));
#if DEBUG
                        app.Context.Trace.Write("ModuleRewriter", String.Concat("Rewriting URL to ", sendToUrl));
#endif
                        RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                        break;
                    }
                }
                if (is404)
                {
                    app.Response.StatusCode      = 404;
                    app.Response.SuppressContent = true;
                    app.Response.End();
                }
            }
            #endregion
        }