博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# URL 重写与反重写
阅读量:6164 次
发布时间:2019-06-21

本文共 6014 字,大约阅读时间需要 20 分钟。

在URL重写中,最重要的莫过于IHttpModule接口了,先看下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; namespace YouXi.Components {
public class HttpModule : IHttpModule {
#region IHttpModule 成员 void IHttpModule.Dispose() {
} void IHttpModule.Init(HttpApplication context) {
//在页面读取开始时,这里每个页面都会先运行 context.BeginRequest += new EventHandler(context_BeginRequest); } void context_BeginRequest(object sender, EventArgs e) {
//这儿处理url重写 UrlRewriter.Rewriter(); } #endregion } } UrlRewriter类,重写URL,并设置重写路径, using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; namespace YouXi.Components {
public class UrlRewriter {
/// /// url路径重写 /// /// public static void Rewriter() {
//这儿处理url重写 //string s_NewUrl = null; //bool IsMatch = false; HttpRequest req = HttpContext.Current.Request; string rawUrl = req.Url.ToString();//.Remove(0, 7).ToLower(); if (!rawUrl.EndsWith(".shtm")) return; if(CommonHelper.UrlReWriterHelper.Rewrite(ref rawUrl)) HttpContext.Current.RewritePath(rawUrl); } } } 重写初始化类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Xml; using System.Text.RegularExpressions; namespace CommonHelper {
public class UrlReWriterHelper {
private static string configFile = "/Configs/UrlReWriter.Config"; public List
Rules; private static UrlReWriterHelper Instance { get; set; } private bool _Enabled; private bool _Reverse; ///
/// 反向匹配 /// public bool Reverse {
get { return _Reverse; } } ///
/// 正向匹配 /// public bool Enabled {
get { return _Enabled; } } ///
/// 初始化重写规则 /// static UrlReWriterHelper() {
Instance = new UrlReWriterHelper(); Instance.Rules = new List
(); string file = HttpContext.Current.Server.MapPath(configFile); XmlDocument xd = new XmlDocument(); xd.Load(file); XmlElement xe = xd.DocumentElement; bool enabled = true; if (xe.Attributes["enabled"] != null) {
enabled = Boolean.TryParse(xe.Attributes["enabled"].Value, out enabled); } Instance._Enabled = enabled; bool reverse = true; if (xe.Attributes["reverse"] != null) {
reverse = Boolean.TryParse(xe.Attributes["reverse"].Value, out reverse); } Instance._Reverse = reverse; foreach (XmlNode xn in xe.ChildNodes) {
if (xn.NodeType == XmlNodeType.Element) {
UrlReWriterRule re = new UrlReWriterRule(xn); Instance.Rules.Add(re); } } } ///
/// 按配置的规则进行路径重写并将最后值改写给参数,返回值表示是否进行重写 /// ///
可以根据此值进行重写操作 ///
public static bool Rewrite(ref string rawUrl) {
if (string.IsNullOrEmpty(rawUrl.Trim())) return false; UrlReWriterHelper config = UrlReWriterHelper.Instance; if (!config.Enabled) return false; List
Rules = config.Rules; if (Rules == null || Rules.Count == 0) return false; foreach (UrlReWriterRule rule in Rules) {
Regex r = new Regex(rule.Pattern, RegexOptions.IgnoreCase); if (r.IsMatch(rawUrl)) {
string s_url = r.Replace(rawUrl, rule.Url); rawUrl = s_url.Replace("/","/"); int start = rawUrl.IndexOf("/", 7); if (start == 0) return true; rawUrl = rawUrl.Substring(start); return true; } } return false; } } } UrlReWriterRule规则类, using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace CommonHelper {
///
/// URL重写配置项 /// public class UrlReWriterRule {
#region private string _Pattern; private string _Url; private string _RePattern; private string _ReUrl; #endregion #region ///
/// 反向重写后的路径 /// public string ReUrl {
get { return _ReUrl; } } ///
/// 反向原始路径 /// public string Repattern {
get { return _RePattern; } } ///
/// 正向重写后的路径 /// public string Url {
get { return _Url; } } ///
/// 正向原始路径 /// public string Pattern {
get { return _Pattern; } } #endregion public UrlReWriterRule() {
} ///
/// 构造函数 /// ///
XML节点 public UrlReWriterRule(XmlNode xn) {
try {
if (xn.SelectSingleNode("pattern") != null) _Pattern = xn.SelectSingleNode("pattern").InnerText; else if(xn.Attributes["pattern"]!=null) _Pattern = xn.Attributes["pattern"].Value; if (xn.SelectSingleNode("url") != null) _Url = xn.SelectSingleNode("url").InnerText; else if (xn.Attributes["url"] != null) _Url = xn.Attributes["url"].Value; if (xn.SelectSingleNode("repattern") != null) _RePattern = xn.SelectSingleNode("repattern").InnerText; else if (xn.Attributes["repattern"] != null) _RePattern = xn.Attributes["repattern"].Value; if (xn.SelectSingleNode("reurl") != null) _ReUrl = xn.SelectSingleNode("reurl").InnerText; else if (xn.Attributes["reurl"] != null) _ReUrl = xn.Attributes["reurl"].Value; } catch (Exception e) {
throw (e); } } } } UrlReWriter.Config 重写规则config文件

 

转载地址:http://vxkba.baihongyu.com/

你可能感兴趣的文章
HTML条件注释判断IE<!--[if IE]><!--[if lt IE 9]>
查看>>
发布和逸出-构造过程中使this引用逸出
查看>>
使用SanLock建立简单的HA服务
查看>>
Subversion使用Redmine帐户验证简单应用、高级应用以及优化
查看>>
Javascript Ajax 异步请求
查看>>
DBCP连接池
查看>>
cannot run programing "db2"
查看>>
mysql做主从relay-log问题
查看>>
Docker镜像与容器命令
查看>>
批量删除oracle中以相同类型字母开头的表
查看>>
Java基础学习总结(4)——对象转型
查看>>
BZOJ3239Discrete Logging——BSGS
查看>>
SpringMVC权限管理
查看>>
spring 整合 redis 配置
查看>>
cacti分组发飞信模块开发
查看>>
浅析LUA中游戏脚本语言之魔兽世界
查看>>
飞翔的秘密
查看>>
Red Hat 安装源包出错 Package xxx.rpm is not signed
查看>>
编译安装mysql-5.6.16.tar.gz
查看>>
活在当下
查看>>