一.功能说明:
可以解决类似 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>
<lookfor>door</lookfor>
<sendto>about/index_292.html</sendto>
<lookfor>kolani</lookfor>
<sendto>index_kolani.html</sendto>
<lookfor>nature</lookfor>
<sendto>index_nature.html</sendto>
<lookfor>mobile</lookfor>
<sendto>index_mobile.html</sendto>
</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上增加通配符配置;
实际使用过程中,可能需要您的匹配规则进行相应的修改,代码仅供参考。