IIS Url ReWriting 示例

 

這個由微軟提供的組件已經編譯好了的,無需自己再寫代碼,直接用就行:)

對於Web.Config設置一下:

<configuration>節最開始加上:

None.gif  <configSections>
None.gif    
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
None.gif  
</configSections>

<configuration>節中加上:

None.gif<RewriterConfig>
None.gif 
<Rules>
None.gif   
<RewriterRule>
None.gif   
<LookFor>~/News,(d{4}),(d{1,2}),(d{1,2}),(d{1,9}).aspx</LookFor>
None.gif   
<SendTo>~/news_view.aspx?newsid=$4</SendTo>
None.gif  
</RewriterRule>
None.gif  
<RewriterRule>
None.gif   
<LookFor>~/(d{4})/(d{2})/Default.aspx</LookFor>
None.gif   
<SendTo><![CDATA[~/news_view.aspx?year=$1&month=$2]]></SendTo>
None.gif  
</RewriterRule>
None.gif 
</Rules>
None.gif  
</RewriterConfig>

  
<system.web>節加上:
 
None.gif <httpModules>
None.gif  
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
None.gif 
</httpModules>

處理一下鏈接:

news.aspx
None.gifprivate void Page_Load(object sender, System.EventArgs e)
ExpandedBlockStart.gif  
{
InBlock.gif   
// 在此處放置用戶代碼以初始化頁面
InBlock.gif
   DataTable dt=new DataProvider().GetNewsToIndexPage(1);
InBlock.gif   
//格式化URL
InBlock.gif
   UrlFormat formats=new UrlFormat();
InBlock.gif
InBlock.gif   lbNews.Text 
= "<ul>";
InBlock.gif   
if (dt.Rows.Count>0)
ExpandedSubBlockStart.gif   
{
InBlock.gif    
int counter=0;
InBlock.gif    
while (counter < dt.Rows.Count)
ExpandedSubBlockStart.gif    
{
InBlock.gif     lbNews.Text 
+= "<li><a href="" + formats.NewsUrl((DateTime)dt.Rows[counter]["NewsPubTime"],Convert.ToInt32(dt.Rows[counter]["NewsID"])) + "" target="_blank">" + dt.Rows[counter]["NewsTitle"].ToString() + "</a></li>";
InBlock.gif     counter
++;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gif   
{
InBlock.gif    lbNews.Text
="<li style="color:red">暫無內容</li>";
ExpandedSubBlockEnd.gif   }

InBlock.gif   lbNews.Text 
+= "</ul>";
InBlock.gif   dt.Clear();
InBlock.gif   dt.Dispose();
InBlock.gif   
ExpandedBlockEnd.gif  }

UrlFormat.cs

None.gifpublic class UrlFormat
ExpandedBlockStart.gif 
{
InBlock.gif
InBlock.gif  
public UrlFormat()
ExpandedSubBlockStart.gif  
{
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
public virtual string NewsUrl(DateTime dt,int NewsID)
ExpandedSubBlockStart.gif  
{
InBlock.gif      
return GetUrl("News,{0},{1},{2}," + NewsID + ".aspx",dt.Year,dt.Month,dt.Day);
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
protected virtual string GetUrl(string pattern, params object[] items)
ExpandedSubBlockStart.gif  
{
InBlock.gif   
return string.Format(pattern,items);
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedBlockEnd.gif }



格式化爲:
news,2004,8,13,123.aspx

指向:
news.aspx?參數=some parameter

作用:可以隱藏真實Url,爽!

相關Dll文件下載:http://bbs.mvpcn.net/PostAttachment.aspx?PostID=405

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章