1:asp.net1.1中重写中可删节的问题!!!
如以下的正则表达式:
<rules>
<rewriterrule>
<lookfors>
<lookfor>~/(\d{4})/(\d{2})\.html</lookfor>———<1>
<lookfor>~/(\d{4})/(\d{2})/</lookfor>————–<2>
<lookfor>~/(\d{4})/(\d{2})</lookfor>———–<3>
<lookfor>~/(\d{4})/(\d{2})/index.html</lookfor>—-<4>
</lookfors>
<sendto>~/pro.aspx?year=$1&month=$2</sendto>
</rewriterrule>
</rules>
其中的1,4可以正常映射到对应的页面
可2,3则会出现http404错误!!!
其原因在于iis本身的处理流程,解决办法则是在网站自己重写404处理错误!!!
1:自定义处理404错误的url(在iis中配置,在web.config中的配置对重写无用)
2:在system.web节中添加如下节:
<httphandlers>
<add verb=”*” path=”404.aspx” type=”lt.http404,lt”></add>
</httphandlers>
<httpmodules>
<add type=”lt.rewritemodule,lt” name=”modulerewriter” />
</httpmodules>
源代码如下:
public class http404:system.web.ihttphandler
{
public http404()
{
//
// todo: 在此处添加构造函数逻辑
//
}
#region ihttphandler 成员
public void processrequest(system.web.httpcontext context)
{
// todo: 添加 http404.processrequest 实现
string errorpath=context.request.rawurl.split(new char[]{;})[1];
string apppath=context.request.applicationpath;
int ipos=errorpath.indexof(apppath);
string url=errorpath.substring(ipos+apppath.length );
// if(!url.endswith(“/”))
// {
// url+=”/”;
// }
// url+=”index.html”;
// context.response.write(url);
// context.rewritepath(url);
//context.response.write(url);
url=”~”+url;
string newurl =lt.rewritemodule.geturl(context,url);
//context.response.write(newurl);
if (newurl != null)
{
//cxt.response.filter = new responsefilter(cxt.response.filter,cxt.request.path);
context.response.write(“请求的路径:” + url);
context.response.write(“<br>”);
context.response.write(“转向的目的url:” + newurl);
context.response.write(“<br>”);
context.rewritepath(newurl);
}
else
{
context.response.write(“你请求的资源不存在!!”);
context.response.end ();
}
}
public bool isreusable
{
get
{
// todo: 添加 http404.isreusable getter 实现
return false;
}
}
///////////////配置节处理中的httpmodule如下:
public class rewritemodule:system.web.ihttpmodule
{
public rewritemodule()
{
//
// todo: 在此处添加构造函数逻辑
//
}
#region ihttpmodule 成员
public void init(system.web.httpapplication context)
{
// todo: 添加 rewritemodule.init 实现
context.beginrequest+=new eventhandler(this.rewrite);
}
private static system.xml.xmldocument ruledoc = null;
private static system.xml.xmldocument getruleconfig(system.web.httpcontext app)
{
if (ruledoc == null)
{
ruledoc = new system.xml.xmldocument();
ruledoc.load(app.server.mappath(“~/rule.xml”));
}
return ruledoc;
}
public static string geturl(system.web.httpcontext cxt,string path)
{
system.xml.xmldocument doc = getruleconfig(cxt);
system.xml.xmlnodelist lst= doc.getelementsbytagname(“rewriterrule”);
string pat=””;
foreach (system.xml.xmlnode nd in lst)
{
system.xml.xmlnodelist sub = nd.childnodes[0].childnodes;
foreach(system.xml.xmlnode chk in sub)
{
pat = “^” + chk.innertext+”$”;
system.text.regularexpressions.regex reg = new system.text.regularexpressions.regex(pat, system.text.regularexpressions.regexoptions.compiled | system.text.regularexpressions.regexoptions.ignorecase);
if(reg.ismatch(path))
{
return reg.replace(path, nd.childnodes[1].innertext);
}
}
}
return null;
}
private void rewrite(object sender,eventargs e)
{
system.web.httpcontext cxt =(sender as system.web.httpapplication).context;
if (cxt.request.contenttype != “image/pjpeg”)
{
string type = cxt.request.contenttype.tolower();
string path = cxt.request.path;
string apppath = cxt.request.applicationpath;
path = path.remove(0, apppath.length);
path = “~” + path;
string newurl = geturl(cxt, path.trimend().trimstart());
if (newurl != null)
{
//cxt.response.filter = new responsefilter(cxt.response.filter,cxt.request.path);
cxt.response.write(“请求的路径:” + path);
cxt.response.write(“<br>”);
cxt.response.write(“转向的目的url:” + newurl);
cxt.response.write(“<br>”);
cxt.rewritepath(newurl);
}
//else
//{
// cxt.response.write(cxt.request.path + “<br>”);
// cxt.response.write(“你请求的资源不存在或无权访问!”);
// cxt.response.flush();
// cxt.response.end();
//}
}
}
public void dispose()
{
// todo: 添加 rewritemodule.dispose 实现
}
#endregion
}
———rule.xml的配置如下:
<?xml version=”1.0″ encoding=”utf-8″ ?>
<rules>
<rewriterrule>
<lookfors>
<lookfor>~/(\d{4})/(\d{2})\.html</lookfor>
<lookfor>~/(\d{4})/(\d{2})/</lookfor>
<lookfor>~/(\d{4})/(\d{2})</lookfor>
<lookfor>~/(\d{4})/(\d{2})/index.html</lookfor>
</lookfors>
<sendto>~/pro.aspx?year=$1&month=$2</sendto>
</rewriterrule>
<rewriterrule>
<lookfors>
<lookfor>~/pro.aspx?year=(\d{4})&month=(\d{2})</lookfor>
</lookfors>
<sendto>~/(\d{4})/(\d{2})\.html</sendto>
</rewriterrule>
<rewriterrule>
<lookfors>
<lookfor>~/pc</lookfor>
</lookfors>
<sendto>~/test2.aspx</sendto>
</rewriterrule>
<rewriterrule>
<lookfors>
<lookfor>~/index.html</lookfor>
<lookfor>~/default.html</lookfor>
</lookfors>
<sendto>~/default.aspx</sendto>
</rewriterrule>
</rules>
/////////对于因重写引起的action的改变,请参考本人写的asp.net2.0中的urlmappings的问题!!!!!