欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net用url重寫URLReWriter實(shí)現(xiàn)任意二級(jí)域名

 更新時(shí)間:2008年10月27日 16:18:47   作者:  
Asp.net 用url重寫(URLReWriter)實(shí)現(xiàn)任意二級(jí)域名

為什么我的httpmodule好像沒有起作用?

在httpmodule程序里設(shè)置斷點(diǎn)后,無論怎么樣,流程都沒有從這里走.原因在于,你沒有向web程序注冊(cè)你的httpmodule程序.這個(gè)工作需要在web.config中完成.
<system.web>
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
</system.web>


為什么總是提示我”未知的配置節(jié)RewriterConfig錯(cuò)誤”

這是因?yàn)槟銢]有向web程序注冊(cè)你的RewriterConfig配置節(jié). 這個(gè)工作需要在web.config中完成.
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
然后,你可以在<configuration>里使用RewriterConfig節(jié)配置規(guī)則了.


url是在httpmodule的哪個(gè)部分處理的?

大多的工作是在URLRewriter. ModuleRewriter. Rewrite()方法里.關(guān)鍵階段是這里:
if (re.IsMatch(requestedPath))
很明顯,這個(gè)判斷傳入的url是否是我們要重寫的url,大家接著看,
String sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
這里接受到web.config中配置的要轉(zhuǎn)到的目標(biāo)url
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
在內(nèi)部把url重寫.


我不想把二級(jí)域名寫死在web.config中,而且我要重寫的目標(biāo)url也不能寫死.比如我們有這樣的需要
Love.kerry.com實(shí)際的處理頁面是kerry.com/action.aspx?id=1
call.kerryl.com實(shí)際的處理頁面是kerry.com/action.aspx?id=2
walkwith.kerry.com實(shí)際的處理頁面是kerry.com/walk.aspx
要怎么處理?


這個(gè)時(shí)候,就需要在上面說的那幾個(gè)代碼里做手腳.
if (re.IsMatch(requestedPath))
{

//找到url里的二級(jí)域名
string [] UserHost = app.Request.Url.Host.Split ( new Char [] { '.' } );
string domain2=UserHost [0];

//根據(jù)需要設(shè)定要重寫的目標(biāo)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里配置規(guī)則的時(shí)候,需要這樣
<RewriterRule>
<LookFor>http://(\w+)\.kerry\.com</LookFor>
<SendTo>/test.aspx</SendTo>
</RewriterRule>
(\w+)用來匹配任意字符串
這里的test.aspx隨便寫別的也可以,因?yàn)槲覀兏緵]有用它.


我有好多不確定二級(jí)域名的站點(diǎn),但是每個(gè)站點(diǎn)的頁面確定,每個(gè)二級(jí)域名站點(diǎn)的內(nèi)容實(shí)際上根劇不同的id從數(shù)據(jù)庫(kù)調(diào),
情況如這樣
http://localhost/kerry/action.aspx?id=1 love.kerry.com/walk.aspx

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

現(xiàn)在傳上去,不能顯示id參數(shù),都改成二級(jí)域名的方式. 這個(gè)時(shí)候該怎么辦?

首先配置規(guī)則
<RewriterRule>
<LookFor>http://(\w+)\.kerry \.com\ walk.aspx</LookFor>
<SendTo>/action.aspx</SendTo>
</RewriterRule>
然后在程序里這樣處理
//獲取二級(jí)域名
string [] UserHost = app.Request.Url.Host.Split ( new Char [] { '.' } );
string domain2=UserHost [0];
根據(jù)域名獲得不同的編號(hào)
int id=getIDfromDomain(domain2);
//獲得要轉(zhuǎn)向的基本url
string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
//加上id參數(shù)
if(id>0)
sendToUrl=string.Format ( "{0}?id={1}" , sendToUrl , id );
else
sendToUrl=”error.aspx”;
//重寫
RewriterUtils.RewriteUrl(app.Context, sendToUrl);


如何匹配目錄?寫了一個(gè)lookfor規(guī)則 http://love.kerry.com/,但是在瀏覽器輸入這個(gè)地址, 總是不能正確的重寫,經(jīng)過trace后發(fā)現(xiàn)根本不能匹配,為什么?

首先,我們應(yīng)該知道,瀏覽器實(shí)際上接受的不是http://love.kerry.com/,而是http://love.kerry.com/default.aspx ,因此,你的</LookFor>規(guī)則應(yīng)該這樣寫
<LookFor>
http://love.kerry.com/default.aspx
</LookFor>
這個(gè)default.aspx應(yīng)該是你在iis里配置的默認(rèn)文檔,如果你的是index.aspx或其他奇怪的名字,就寫成你自己的名字
同樣, http://love.kerry.com/fun 這個(gè)地址要匹配,需要這樣的規(guī)則http://love.kerry.com/fun/default.aspx
但是,再羅嗦一句,你的文件根本不需要有fun這個(gè)目錄,因?yàn)?..重寫了嘛


我搜到網(wǎng)上還有另外一種解決辦法…

或許你是指這篇文章

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

大家可以看到,其基本的方法都是一樣的.之所以沒有把這個(gè)列在最前面,是因?yàn)檫@個(gè)做法有些取巧,可能一開始不是那么好理解.但是我相信看到最后的朋友再看這篇文章,應(yīng)該都會(huì)會(huì)心的一笑
Happy programming

相關(guān)文章

最新評(píng)論