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

asp.net用url重寫URLReWriter實現(xiàn)任意二級域名第2/2頁

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

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

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


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

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


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);
在內(nèi)部把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];

//根據(jù)需要設定要重寫的目標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ī)則的時候,需要這樣
<RewriterRule>
<LookFor>http://(\w+)\.kerry\.com</LookFor>
<SendTo>/test.aspx</SendTo>
</RewriterRule>
(\w+)用來匹配任意字符串
這里的test.aspx隨便寫別的也可以,因為我們根本沒有用它.


我有好多不確定二級域名的站點,但是每個站點的頁面確定,每個二級域名站點的內(nèi)容實際上根劇不同的id從數(shù)據(jù)庫調(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ù),都改成二級域名的方式. 這個時候該怎么辦?

首先配置規(guī)則
<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];
根據(jù)域名獲得不同的編號
int id=getIDfromDomain(domain2);
//獲得要轉向的基本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);


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

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


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

或許你是指這篇文章

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

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

相關文章

最新評論