自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET下用URLRewriter重寫(xiě)二級(jí)域名

開(kāi)發(fā) 后端
今天我們將談到的是ASP.NET下用URLRewriter重寫(xiě)二級(jí)域名的問(wèn)題,還涉及到相關(guān)擴(kuò)展庫(kù)的事項(xiàng),希望對(duì)大家有所幫助。

這里要求對(duì)域名進(jìn)行重寫(xiě),實(shí)現(xiàn)http://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)

  1.  protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)   
  2.  {   
  3.  HttpApplication app = (HttpApplication) sender;   
  4.  //Rewrite(app.Request.Path, app);   
  5. Rewrite(app.Request.Url.AbsoluteUri, app); // ## ## ## 這里修改了   
  6. }  

這里將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)下添加如下一行代碼

  1. <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>http://(\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)里的http://(\w+).abc.com/不能少了***的反斜框

OK,一切完工

發(fā)布,上傳到服務(wù)器,測(cè)試一下,如圖

本次測(cè)試需要一個(gè)類(lèi)庫(kù):URLRewriter.dll (測(cè)試版本 1.0.1495.18710)

官網(wǎng)下載地址:http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi

【編輯推薦】

  1. 詳解ASP.NET MVC 3 beta新特性
  2. ASP.NET MVC 3讓依賴(lài)注入實(shí)現(xiàn)得更簡(jiǎn)單
  3. 詳解ASP.NET MVC 3 beta新特性
  4. ASP.NET MVC 3新特性與NuPack功能詳解
  5. .NET開(kāi)發(fā)人員應(yīng)該關(guān)注的七個(gè)開(kāi)源項(xiàng)目

 

責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2011-06-07 16:18:48

網(wǎng)站域名二級(jí)域名

2009-08-05 14:46:17

ASP.NET url

2009-07-31 09:39:59

ASP.NET和URL

2021-11-03 10:52:39

數(shù)據(jù)IP域名

2012-12-27 09:44:41

2009-12-30 14:28:09

ASP.NET Web

2009-08-03 14:22:33

什么是ASP.NET

2009-07-28 17:17:19

ASP.NET概述

2009-07-22 17:45:35

ASP.NET教程

2010-01-12 09:46:55

2009-07-27 12:22:03

ASP.NET和ASPASP.NET入門(mén)教程

2009-08-10 13:32:15

ASP.NET TimASP.NET組件設(shè)計(jì)

2009-07-20 17:17:54

Shell函數(shù)ASP.NET環(huán)境

2009-07-29 17:11:25

ASP.NET ISA

2009-07-29 15:26:43

ASP.NET連接Or

2009-07-29 16:08:07

ASP和ASP.NET

2009-08-03 17:35:07

ASP.NET WebASP.NET編程工具

2009-07-28 09:02:32

asp.net aja

2011-09-07 09:43:04

ASP.NET服務(wù)框架

2009-08-03 13:38:18

ASP.NET編程模型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)