Asp.net URL 重写

http://www.cnblogs.com/fengfeng/archive/2008/02/15/1069462.html

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

namespace URLRewriter
{
    /**//// <summary>
    /// Provides a rewriting HttpModule.
    /// </summary>
    public class ModuleRewriter : BaseModuleRewriter
    {
        /**//// <summary>
        /// This method is called during the module's BeginRequest event.
        /// </summary>
        /// <param name="requestedRawUrl">The RawUrl being requested (includes path and querystring).</param>
        /// <param name="app">The HttpApplication instance.</param>
        protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
        {
            //只对文件后缀为aspx页面有效
            if (requestedPath.IndexOf(".aspx") != -1)
            {
                HttpContext httpContext = app.Context;

                // get the configuration rules
                RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;

                // iterate through each rule
                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(httpContext.Request.ApplicationPath, rules[i].LookFor) + "$";

                    // Create a regex (note that IgnoreCase is set)
                    Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

                    // See if a match is found
                    if (re.IsMatch(requestedPath))
                    {
                        //aspx页面重定向到htm页面且http数据传输方式为“GET”
                        if (rules[i].Type == WebType.Static && app.Context.Request.RequestType == "GET")
                        {
                            //静态页面路径
                            string htmlWeb = RewriterUtils.ResolveUrl(httpContext.Request.ApplicationPath, rules[i].SendTo);
                            //静态页面物理路径
                            string htmPath = httpContext.Server.MapPath(htmlWeb);
                            //静态页面的最后修改时间
                            DateTime dt = System.IO.File.GetLastWriteTime(htmPath);
                            if (DateTime.Compare(DateTime.Now.AddHours(-1), dt) <= 0) //当前时间和静态页面生成时间对比,如果时间小于一个小时,重定向到静态页面
                            {
                                requestedPath = htmlWeb;
                            }
                            else  //当前时间和静态页面生成时间对比,如果时间大于一个小时,设定Response筛选器,用于生成静态页面
                            {
                                httpContext.Response.Filter = new ResponseFilter(httpContext.Response.Filter, htmPath);
                            }
                        }
                        else
                        {
                            requestedPath = rules[i].SendTo;
                        }

                        // Rewrite the URL
                        RewriterUtils.RewriteUrl(httpContext, requestedPath);
                        break;        // exit the for loop
                    }
                }
            }
        }

    }
}

你可能感兴趣的:(Web,UI,.net,asp.net,asp)