实现快速简单高效并可以灵活配置的URL重写方案

80酷酷网    80kuku.com

  

需要源代码请在评论里留下email

关键代码: 
public class MyHttpModule : IHttpModule
 {
  public void Init(HttpApplication app)
  {
   app.AuthorizeRequest += new EventHandler(app_AuthorizeRequest);
  }

  public void Dispose() {}

  protected void Rewrite(string requestedPath, System.Web.HttpApplication app)
  {
   //   app.Context.RewritePath("~/default.aspx", string.Empty, "test=tttttttt");
   foreach(URLRewrite url in SiteUrls.GetSiteUrls().Urls)
   {
    if (Regex.IsMatch(app.Context.Request.Path, url.Pattern, RegexOptions.Compiled|RegexOptions.IgnoreCase))
    {
     app.Context.RewritePath(url.Page, string.Empty, Regex.Replace(app.Context.Request.Path, url.Pattern, url.QueryString, RegexOptions.Compiled|RegexOptions.IgnoreCase));
     return;
    }
   }
   if (app.Context.Request.Path.ToLower().EndsWith(".shtml"))
   {
    app.Context.Response.Redirect("~/index.html");
   }
  }

  private void app_AuthorizeRequest(object sender, EventArgs e)
  {
   HttpApplication app = (HttpApplication) sender;
   Rewrite(app.Request.Path, app);
  }
 }

 public class SiteUrls
 {
  #region 内部属性和方法
  string SiteUrlsFile = HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["SiteUrls"]);
  private ArrayList _Urls;
  public ArrayList Urls
  {
   get
   {
    return _Urls;
   }
   set
   {
    _Urls = value;
   }
  }

  private NameValueCollection _Paths;
  public NameValueCollection Paths
  {
   get
   {
    return _Paths;
   }
   set
   {
    _Paths = value;
   }
  }
  
  private SiteUrls()
  {
   string applicationPath = HttpContext.Current.Request.ApplicationPath;

   if (applicationPath == "/")
   {
    applicationPath = string.Empty;
   }

   Urls = new ArrayList();
   Paths = new NameValueCollection();
   Paths.Add("home", applicationPath);

   XmlDocument xml = new XmlDocument();

   xml.Load(SiteUrlsFile);

   XmlNode root = xml.SelectSingleNode("SiteUrls");
   foreach(XmlNode n in root.ChildNodes)
   {
    if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "rewrite")
    {
     XmlAttribute name = n.Attributes["name"];
     XmlAttribute path = n.Attributes["path"];
     XmlAttribute page = n.Attributes["page"];
     XmlAttribute querystring = n.Attributes["querystring"];
     XmlAttribute pattern = n.Attributes["pattern"];

     if (name != null && path != null && page != null && querystring != null && pattern != null)
     {
      Paths.Add(name.Value, applicationPath + path.Value);
      Urls.Add(new URLRewrite(name.Value, Paths["home"]+pattern.Value, Paths["home"]+page.Value.Replace("^", "&"), querystring.Value.Replace("^", "&")));
     }
    }
   }
  }
  #endregion

  public static SiteUrls GetSiteUrls()
  {
   string CacheKey = "SiteUrls";
   SiteUrls urls = System.Web.HttpContext.Current.Cache["SiteUrls"] as SiteUrls;
   if (urls == null)
   {
    urls = new SiteUrls();
    System.Web.HttpContext.Current.Cache.Insert(CacheKey, urls, new CacheDependency(urls.SiteUrlsFile), DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.High, null);
   }

   return urls;
  }

  /// <summary>
  /// 输出URL示例
  /// </summary>
  /// <param name="id"></param>
  /// <returns></returns>
  public string Show(int id)
  {
   return string.Format(Paths["Show"], id);
  }

 public class URLRewrite
 {
  #region 成员变量
  private string _Name;
  public string Name
  {
   get
   {
    return _Name;
   }
   set
   {
    _Name = value;
   }
  }

  private string _Pattern;
  public string Pattern
  {
   get
   {
    return _Pattern;
   }
   set
   {
    _Pattern = value;
   }
  }

  private string _Page;
  public string Page
  {
   get
   {
    return _Page;
   }
   set
   {
    _Page = value;
   }
  }

  private string _QueryString;
  public string QueryString
  {
   get
   {
    return _QueryString;
   }
   set
   {
    _QueryString = value;
   }
  }
  #endregion

  #region 构造函数
  public URLRewrite(string name, string pattern, string page, string querystring)
  {
   _Name = name;
   _Pattern = pattern;
   _Page = page;
   _QueryString = querystring;
  }
  #endregion
 }

 public class PageBase : Page
 {
  private PageBase()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  //// <summary>
  ///  重写默认的HtmlTextWriter方法,修改form标记中的value属性,使其值为重写的URL而不是真实URL。
  /// </summary>
  /// <param name="writer"></param>
  protected override void Render(HtmlTextWriter writer)
  {

   if (writer is System.Web.UI.Html32TextWriter)
   {
    writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
   }
   else
   {
    writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
   }

   base.Render(writer);
  }
 }

 internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
 {
  private string _url; // 假的URL

  internal FormFixerHtml32TextWriter(TextWriter writer):base(writer)
  {
   _url = HttpContext.Current.Request.RawUrl;
  }

  public override void WriteAttribute(string name, string value, bool encode)
  {
   // 如果当前输出的属性为form标记的action属性,则将其值替换为重写后的虚假URL
   if (_url != null && string.Compare(name, "action", true) == 0)
   {
    value = _url;
   }
   base.WriteAttribute(name, value, encode);
  }
 }

    
 internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
 {
  private string _url;
  internal FormFixerHtmlTextWriter(TextWriter writer):base(writer)
  {
   _url = HttpContext.Current.Request.RawUrl;
  }

  public override void WriteAttribute(string name, string value, bool encode)
  {
   if (_url != null && string.Compare(name, "action", true) == 0)
   {
    value = _url;
   }

   base.WriteAttribute(name, value, encode);
  }
 }

重写配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<SiteUrls>
 <!--如果重写shtml扩展名,需要在IIS里,调整应用程序映射,把shtml扩展名映射到C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,注意取消检查文件是否存在-->
 <!--访问方式-->
    <rewrite name="Show"
          path="/Show/{0}.shtml"
          pattern = "/Show/(\d+).shtml"
          page="/WebForm1.aspx"
          querystring="id=$1^cn=ItemList" />

</SiteUrls>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: