轉(zhuǎn)帖|其它|編輯:郝浩|2011-03-24 13:29:36.000|閱讀 393 次
概述:今天我們將談到的是ASP.NET下用URLRewriter重寫(xiě)二級(jí)域名的問(wèn)題,還涉及到相關(guān)擴(kuò)展庫(kù)的事項(xiàng),希望對(duì)大家有所幫助。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
這里要求對(duì)域名進(jìn)行重寫(xiě),實(shí)現(xiàn)//1234.abc.com/ 到 ~/Defa.aspx?id=1234的重寫(xiě)。
第一:域名
首先域名要支持泛解悉,就是域名解悉的主機(jī)名為星號(hào)*,例:*.abc.com。如下圖
這樣能保證你在瀏覽器地址欄輸入任何前綴,DNS都會(huì)把它們指向到你指定的IP地址上。
第二:IIS設(shè)置(Win2003 + IIS 6為例)
(1)網(wǎng)站必須為Web服務(wù)器的默認(rèn)站點(diǎn),即端口號(hào)為80,主機(jī)頭為空的站點(diǎn)。如下圖所示。
該站點(diǎn)接收所有對(duì)該服務(wù)器的HTTP請(qǐng)求(其它設(shè)置為主機(jī)頭的站點(diǎn)除外)。所以任何二級(jí)域名訪問(wèn)該服務(wù)器都會(huì)由該站點(diǎn)進(jìn)行處理。
(2)另外要在站點(diǎn)的“通配符應(yīng)用程序映射”列表中添加ASP.NET的Web請(qǐng)求處理程序aspnet_isapi.dll。如下圖所示。
在這里的設(shè)置,是讓該站點(diǎn)接到的所有請(qǐng)求都交給aspnet_isapi.dll處理。
第三:修改Microsoft的URLRewriter。
運(yùn)行開(kāi)源項(xiàng)目URLRewriter。這里需要修改兩個(gè)地方:
(1)BaseModuleRewriter.cs類(lèi)
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
//Rewrite(app.Request.Path, app);
Rewrite(app.Request.Url.AbsoluteUri, app); // ## ## ## 這里修改了
}
這里將app.Request.Path 替換成了 app.Request.Url.AbsoluteUri
(2)ModuleRewriter.cs類(lèi)
1. protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
2. {
3. // log information to the Trace object.
4. app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");
5. // get the configuration rules
6. RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
7. // iterate through each rule...
8. for (int i = 0; i < rules.Count; i++)
9. {
10. // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
11. //string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
12. string lookFor = "^" + rules[i].LookFor + "$"; // ## ## ## 這里修改了
13. // Create a regex (note that IgnoreCase is set...)
14. Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
15. // See if a match is found
16. if (re.IsMatch(requestedPath))
17. {
18. // match found - do any replacement needed
19. string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
20. // log rewriting information to the Trace object
21. app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
22. // Rewrite the URL
23. RewriterUtils.RewriteUrl(app.Context, sendToUrl);
24. break; // exit the for loop
25. }
26. }
27. // Log information to the Trace object
28. app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");
29. }
這里將string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
改成了 string lookFor = "^" + rules[i].LookFor + "$";
這兩個(gè)地方修改完以后,生成項(xiàng)目。將項(xiàng)止目bin/Debug目錄下的URLRewriter.dll文件Copy到我們要重寫(xiě)URL的項(xiàng)目中。
第四:配置項(xiàng)目
(1)在web.config文件的configSections節(jié)點(diǎn)下添加如下一行代碼
<section name="RewriterConfig"
type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
這里配置一個(gè)重寫(xiě)配置的類(lèi)
(2)修改httpModules節(jié)點(diǎn),在里面添加一行配置代碼
1. <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
(3)在主節(jié)點(diǎn)configuration節(jié)點(diǎn)下添加路由規(guī)則,代碼如下:
1. <!-- URL重寫(xiě) 將捕獲頁(yè)面轉(zhuǎn)發(fā)到實(shí)際地址 -->
2. <RewriterConfig>
3. <Rules>
4. <RewriterRule>
5. <LookFor>//(\w+).abc.com/</LookFor>
6. <SendTo>~/Defa.aspx?id=$1</SendTo>
7. </RewriterRule>
8. </Rules>
9. </RewriterConfig>
10. <!-- URL重寫(xiě) 將捕獲頁(yè)面轉(zhuǎn)發(fā)到實(shí)際地址 ( 結(jié)束 ) -->
代碼里一個(gè)RewriterRule節(jié)點(diǎn)就是一個(gè)規(guī)則,這時(shí)只有一個(gè),即把域名中的主機(jī)頭部分做為Defa.aspx頁(yè)面的id參數(shù)的值發(fā)送給Defa.aspx頁(yè)面。
注意:這里L(fēng)ookFor節(jié)點(diǎn)里的//(\w+).abc.com/不能少了最后的反斜框
OK,一切完工
發(fā)布,上傳到服務(wù)器,測(cè)試一下,如圖
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載