ASP.NET 自定义URL重写 分类: ASP.NET 2014-10-31 16:05 174人阅读 评论(0) 收藏
2014-10-31 16:05
363 查看
一.功能说明:
可以解决类似 http://****/news 情形,Url路径支持正则匹配。
二.操作步骤:
1.增加URL重写模块:
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
/// <summary>
/// URL重写Module
/// </summary>
public class UrlRewriteModule : IHttpModule
{
#region IHttpModule Members
public virtual void Init(HttpApplication context)
{
context.BeginRequest += ApplicationBeginRequest;
}
public virtual void Dispose()
{
}
#endregion
public bool IsExcludedPath(string relUrl)
{
string fileExt = Path.GetExtension(relUrl);
if (!string.IsNullOrEmpty(fileExt)
&& (fileExt.ToLower() == ".axd" ||
fileExt.ToLower() == ".jpg" ||
fileExt.ToLower() == ".png" ||
fileExt.ToLower() == ".gif" ||
fileExt.ToLower() == ".swf" ||
fileExt.ToLower() == ".bmp"
))
{
return true;
}
return false;
}
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
public void ApplicationBeginRequest(object source, EventArgs e)
{
var application = (HttpApplication)source;
HttpContext context = application.Context;
try
{
string path = context.Request.Path;
string file = Path.GetFileName(path);
if (IsExcludedPath(path))
{
return;
}
if (file != null && HttpContext.Current != null)
{
string rewriteConfig = HttpContext.Current.Server.MapPath("~/Config/RewriterConfig.config");
if (File.Exists(rewriteConfig))
{
var xml = new XmlDocument();
xml.Load(rewriteConfig);
XmlNodeList rules = xml.SelectNodes("RewriterConfig/Rules/RewriterRule");
if (rules != null)
{
foreach (XmlNode rule in rules)
{
string lookFor = "";
string sendTo = "";
XmlNode lookForNode = rule.SelectSingleNode("LookFor");
if (lookForNode != null)
{
lookFor = lookForNode.InnerText;
}
XmlNode sendToNode = rule.SelectSingleNode("SendTo");
if (sendToNode != null)
{
sendTo = sendToNode.InnerText;
}
if (!string.IsNullOrEmpty(lookFor) && !string.IsNullOrEmpty(sendTo))
{
string regeRule = Regex.Escape(lookFor);
var regex = new Regex("^(?i)" + regeRule + "$", RegexOptions.Compiled);
//匹配无后缀时路径
if (string.IsNullOrEmpty(file))
{
if (context.Request.ApplicationPath != null)
{
var subPath = path.Substring(context.Request.ApplicationPath.Length).TrimStart('/').TrimEnd('/');
if (regex.Match(subPath).Success)
{
context.RewritePath(Path.Combine(context.Request.ApplicationPath, sendTo));
break;
}
}
}
else
{
if (regex.Match(file).Success)
{
context.RewritePath(sendTo);
break;
}
}
}
}
}
}
}
}
catch (Exception ex)
{
context.Response.Clear();
context.Response.Write(ex.Message);
context.Response.End();
}
}
}
2.增加Url重写配置,放到网站根目录下Config文件夹下:~/Config/RewriterConfig.config
<?xml version="1.0"?>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>floor</LookFor>
<SendTo>index_floor.html</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>door</LookFor>
<SendTo>about/index_292.html</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>kolani</LookFor>
<SendTo>index_kolani.html</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>nature</LookFor>
<SendTo>index_nature.html</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>mobile</LookFor>
<SendTo>index_mobile.html</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
3.在webconfig里注册HttpModule;注意有2个地方需要处理
集成模式下:
<system.webServer>
<modules>
<!--Url重写-->
<add name="UrlRewriteModule" type="UrlRewriteModule" />
经典模式:在config/HttpModules.config里
<httpModules>
<!--Url重写-->
<add name="UrlRewriteModule" type="UrlRewriteModule" />
4.如果是无后缀路径,比如/news,IIS6时需在IIS上增加通配符配置;
实际使用过程中,可能需要您的匹配规则进行相应的修改,代码仅供参考。
可以解决类似 http://****/news 情形,Url路径支持正则匹配。
二.操作步骤:
1.增加URL重写模块:
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
/// <summary>
/// URL重写Module
/// </summary>
public class UrlRewriteModule : IHttpModule
{
#region IHttpModule Members
public virtual void Init(HttpApplication context)
{
context.BeginRequest += ApplicationBeginRequest;
}
public virtual void Dispose()
{
}
#endregion
public bool IsExcludedPath(string relUrl)
{
string fileExt = Path.GetExtension(relUrl);
if (!string.IsNullOrEmpty(fileExt)
&& (fileExt.ToLower() == ".axd" ||
fileExt.ToLower() == ".jpg" ||
fileExt.ToLower() == ".png" ||
fileExt.ToLower() == ".gif" ||
fileExt.ToLower() == ".swf" ||
fileExt.ToLower() == ".bmp"
))
{
return true;
}
return false;
}
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
public void ApplicationBeginRequest(object source, EventArgs e)
{
var application = (HttpApplication)source;
HttpContext context = application.Context;
try
{
string path = context.Request.Path;
string file = Path.GetFileName(path);
if (IsExcludedPath(path))
{
return;
}
if (file != null && HttpContext.Current != null)
{
string rewriteConfig = HttpContext.Current.Server.MapPath("~/Config/RewriterConfig.config");
if (File.Exists(rewriteConfig))
{
var xml = new XmlDocument();
xml.Load(rewriteConfig);
XmlNodeList rules = xml.SelectNodes("RewriterConfig/Rules/RewriterRule");
if (rules != null)
{
foreach (XmlNode rule in rules)
{
string lookFor = "";
string sendTo = "";
XmlNode lookForNode = rule.SelectSingleNode("LookFor");
if (lookForNode != null)
{
lookFor = lookForNode.InnerText;
}
XmlNode sendToNode = rule.SelectSingleNode("SendTo");
if (sendToNode != null)
{
sendTo = sendToNode.InnerText;
}
if (!string.IsNullOrEmpty(lookFor) && !string.IsNullOrEmpty(sendTo))
{
string regeRule = Regex.Escape(lookFor);
var regex = new Regex("^(?i)" + regeRule + "$", RegexOptions.Compiled);
//匹配无后缀时路径
if (string.IsNullOrEmpty(file))
{
if (context.Request.ApplicationPath != null)
{
var subPath = path.Substring(context.Request.ApplicationPath.Length).TrimStart('/').TrimEnd('/');
if (regex.Match(subPath).Success)
{
context.RewritePath(Path.Combine(context.Request.ApplicationPath, sendTo));
break;
}
}
}
else
{
if (regex.Match(file).Success)
{
context.RewritePath(sendTo);
break;
}
}
}
}
}
}
}
}
catch (Exception ex)
{
context.Response.Clear();
context.Response.Write(ex.Message);
context.Response.End();
}
}
}
2.增加Url重写配置,放到网站根目录下Config文件夹下:~/Config/RewriterConfig.config
<?xml version="1.0"?>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>floor</LookFor>
<SendTo>index_floor.html</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>door</LookFor>
<SendTo>about/index_292.html</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>kolani</LookFor>
<SendTo>index_kolani.html</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>nature</LookFor>
<SendTo>index_nature.html</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>mobile</LookFor>
<SendTo>index_mobile.html</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
3.在webconfig里注册HttpModule;注意有2个地方需要处理
集成模式下:
<system.webServer>
<modules>
<!--Url重写-->
<add name="UrlRewriteModule" type="UrlRewriteModule" />
经典模式:在config/HttpModules.config里
<httpModules>
<!--Url重写-->
<add name="UrlRewriteModule" type="UrlRewriteModule" />
4.如果是无后缀路径,比如/news,IIS6时需在IIS上增加通配符配置;
实际使用过程中,可能需要您的匹配规则进行相应的修改,代码仅供参考。
相关文章推荐
- ASP.NET 自定义URL重写 分类: ASP.NET 2014-10-31 16:05 175人阅读 评论(0) 收藏
- 简单的ASP.NET无刷新分页 分类: ASP.NET 2012-05-06 02:09 655人阅读 评论(1) 收藏
- 【ASP.NET开发】ASP.NET对SQLServer的通用数据库访问类 分类: ASP.NET 2012-09-20 11:17 2768人阅读 评论(0) 收藏
- ASP.NET中登录页验证码的实现 分类: ASP.NET 2012-03-08 22:44 4936人阅读 评论(5) 收藏
- 解析ASP.NET Mvc开发之EF延迟加载 分类: ASP.NET 2014-01-04 01:29 4017人阅读 评论(1) 收藏
- 解析ASP.NET Mvc开发之查询数据实例 分类: ASP.NET 2014-01-02 01:27 5788人阅读 评论(3) 收藏
- 【ASP.NET开发】ASP.NET(MVC)三层架构知识的学习总结 分类: ASP.NET 2012-09-27 17:09 1985人阅读 评论(1) 收藏
- 【ASP.NET开发】ADO.NET调用带输出参数的存储过程 分类: ASP.NET 2012-09-10 21:16 1261人阅读 评论(1) 收藏
- 解析ASP.NET Mvc开发之删除修改数据 分类: ASP.NET 2014-01-04 23:41 3203人阅读 评论(2) 收藏
- 【ASP.NET开发】.NET三层架构简单解析 分类: ASP.NET 2013-01-16 18:05 1568人阅读 评论(0) 收藏
- DWZ和ASP.NET MVC等框架的冲突问题解决方案 分类: 技巧总结 2015-04-07 14:46 188人阅读 评论(0) 收藏
- 解析ASP.NET WebForm和Mvc开发的区别 分类: ASP.NET 2013-12-29 01:59 11738人阅读 评论(5) 收藏
- Jquery easy UI 上中下三栏布局 分类: ASP.NET 2015-02-06 09:19 368人阅读 评论(0) 收藏
- Jquery easy UI 上中下三栏布局 分类: ASP.NET 2015-02-06 09:19 370人阅读 评论(0) 收藏
- 百度编辑器UEditor ASP.NET示例Demo 分类: ASP.NET 2015-01-12 11:18 346人阅读 评论(0) 收藏
- IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏
- 网络工作室暑假后第一次培训资料(ADO.NET创建访问数据集)整理 分类: ASP.NET 2012-10-05 20:10 911人阅读 评论(0) 收藏
- 百度编辑器UEditor ASP.NET示例Demo 分类: ASP.NET 2015-01-12 11:18 347人阅读 评论(0) 收藏
- IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 59人阅读 评论(0) 收藏
- 网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 614人阅读 评论(0) 收藏