UrlRewriter/URLRewriter.Module.cs

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Web;
using System.Text.RegularExpressions;
using System.Configuration;
using URLRewriter.Config;

namespace URLRewriter
{
    public class RewriterModule : IHttpModule
	{
		public void Init(HttpApplication app)
		{
            // WARNING!  This does not work with Windows authentication!
			// If you are using Windows authentication, change to app.BeginRequest
            app.AuthorizeRequest += new EventHandler(this.URLRewriter);
		}

        protected void URLRewriter(object sender, EventArgs e)
		{
			HttpApplication app = (HttpApplication) sender;
            string requestedPath = app.Request.Path;
		
            // get the configuration rules
            UrlsCollection rules = UrlsConfig.GetConfig().Urls;

            for (int i = 0; i < rules.Count; i++)
            {
                // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].VirtualUrl) + "$";

                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
                if (re.IsMatch(requestedPath))
                {
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].DestinationUrl));
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    break;
                }
            }
        
        }

        public void Dispose() { }
    }



    /// <summary>
    /// Provides utility helper methods for the rewriting HttpModule and HttpHandler.
    /// </summary>
    /// <remarks>This class is marked as internal, meaning only classes in the same assembly will be
    /// able to access its methods.</remarks>
    internal class RewriterUtils
    {
        #region RewriteUrl
        /// <summary>
        /// Rewrite's a URL using <b>HttpContext.RewriteUrl()</b>.
        /// </summary>
        /// <param name="context">The HttpContext object to rewrite the URL to.</param>
        /// <param name="sendToUrl">The URL to rewrite to.</param>
        internal static void RewriteUrl(HttpContext context, string sendToUrl)
后面还有 79 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。