当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net用url重写URLReWriter实现任意二级域名第1/2页

asp.net用url重写URLReWriter实现任意二级域名第1/2页

2018年04月20日  | 移动技术网IT编程  | 我要评论

为什么我的httpmodule好像没有起作用?

在httpmodule程序里设置断点后,无论怎么样,流程都没有从这里走.原因在于,你没有向web程序注册你的httpmodule程序.这个工作需要在web.config中完成.
<system.web>
<httpmodules>
<add type="urlrewriter.modulerewriter, urlrewriter" name="modulerewriter" />
</httpmodules>
</system.web>


为什么总是提示我”未知的配置节rewriterconfig错误”

这是因为你没有向web程序注册你的rewriterconfig配置节. 这个工作需要在web.config中完成.
<configsections>
<section name="rewriterconfig" type="urlrewriter.config.rewriterconfigserializersectionhandler, urlrewriter" />
</configsections>
然后,你可以在<configuration>里使用rewriterconfig节配置规则了.


url是在httpmodule的哪个部分处理的?

大多的工作是在urlrewriter. modulerewriter. rewrite()方法里.关键阶段是这里:
if (re.ismatch(requestedpath))
很明显,这个判断传入的url是否是我们要重写的url,大家接着看,
string sendtourl = rewriterutils.resolveurl(app.context.request.applicationpath, re.replace(requestedpath, rules[i].sendto));
这里接受到web.config中配置的要转到的目标url
rewriterutils.rewriteurl(app.context, sendtourl);
在内部把url重写.


我不想把二级域名写死在web.config中,而且我要重写的目标url也不能写死.比如我们有这样的需要
love.kerry.com实际的处理页面是kerry.com/action.aspx?id=1
call.kerryl.com实际的处理页面是kerry.com/action.aspx?id=2
walkwith.kerry.com实际的处理页面是kerry.com/walk.aspx
要怎么处理?


这个时候,就需要在上面说的那几个代码里做手脚.
if (re.ismatch(requestedpath))
{

//找到url里的二级域名
string [] userhost = app.request.url.host.split ( new char [] { '.' } );
string domain2=userhost [0];

//根据需要设定要重写的目标url
string sendtourl ;
if(domain2==” love”)
sendtourl =” /action.aspx?id=1”;
else if(domain2==” call”)
sendtourl =” /action.aspx?id=2”;
else i f(domain2==” walkwith”)
sendtourl =” /walk.aspx”;

rewriterutils.rewriteurl(app.context, sendtourl);

}

在web.config里配置规则的时候,需要这样
<rewriterrule>
<lookfor>http://(\w+)\.kerry\.com</lookfor>
<sendto>/test.aspx</sendto>
</rewriterrule>
(\w+)用来匹配任意字符串
这里的test.aspx随便写别的也可以,因为我们根本没有用它.


我有好多不确定二级域名的站点,但是每个站点的页面确定,每个二级域名站点的内容实际上根剧不同的id从数据库调,
情况如这样
http://localhost/kerry/action.aspx?id=1 love.kerry.com/walk.aspx

http://localhost/kerry/action.aspx?id=14 like.kerry.com/walk.aspx

现在传上去,不能显示id参数,都改成二级域名的方式. 这个时候该怎么办?

首先配置规则
<rewriterrule>
<lookfor>http://(\w+)\.kerry \.com\ walk.aspx</lookfor>
<sendto>/action.aspx</sendto>
</rewriterrule>
然后在程序里这样处理
//获取二级域名
string [] userhost = app.request.url.host.split ( new char [] { '.' } );
string domain2=userhost [0];
根据域名获得不同的编号
int id=getidfromdomain(domain2);
//获得要转向的基本url
string sendtourl = rewriterutils.resolveurl(app.context.request.applicationpath, re.replace(requestedpath, rules[i].sendto));
//加上id参数
if(id>0)
sendtourl=string.format ( "{0}?id={1}" , sendtourl , id );
else
sendtourl=”error.aspx”;
//重写
rewriterutils.rewriteurl(app.context, sendtourl);


如何匹配目录?写了一个lookfor规则 http://love.kerry.com/,但是在浏览器输入这个地址, 总是不能正确的重写,经过trace后发现根本不能匹配,为什么?

首先,我们应该知道,浏览器实际上接受的不是http://love.kerry.com/,而是http://love.kerry.com/default.aspx ,因此,你的</lookfor>规则应该这样写
<lookfor>
http://love.kerry.com/default.aspx
</lookfor>
这个default.aspx应该是你在iis里配置的默认文档,如果你的是index.aspx或其他奇怪的名字,就写成你自己的名字
同样, http://love.kerry.com/fun 这个地址要匹配,需要这样的规则http://love.kerry.com/fun/default.aspx
但是,再罗嗦一句,你的文件根本不需要有fun这个目录,因为...重写了嘛


我搜到网上还有另外一种解决办法…

或许你是指这篇文章

http://blog.csdn.net/mengyao/archive/2007/01/25/1493537.aspx

大家可以看到,其基本的方法都是一样的.之所以没有把这个列在最前面,是因为这个做法有些取巧,可能一开始不是那么好理解.但是我相信看到最后的朋友再看这篇文章,应该都会会心的一笑
happy programming
2

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网