这个由微软提供的组件已经编译好了的,无需自己再写代码,直接用就行:)
对于Web.Config设置一下:
在<configuration>节最开始加上:
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
在<configuration>节中加上:
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/News,(d{4}),(d{1,2}),(d{1,2}),(d{1,9}).aspx</LookFor>
<SendTo>~/news_view.aspx?newsid=$4</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(d{4})/(d{2})/Default.aspx</LookFor>
<SendTo><![CDATA[~/news_view.aspx?year=$1&month=$2]]></SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
在<system.web>节加上:
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
处理一下链接:
news.aspx
private void Page_Load(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// 在此处放置用户代码以初始化页面
DataTable dt=new DataProvider().GetNewsToIndexPage(1);
//格式化URL
UrlFormat formats=new UrlFormat();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbNews.Text = "<ul>";
if (dt.Rows.Count>0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int counter=0;
while (counter < dt.Rows.Count)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.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>";
counter++;
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lbNews.Text="<li style="color:red">暂无内容</li>";
}
lbNews.Text += "</ul>";
dt.Clear();
dt.Dispose();
}
UrlFormat.cs
public class UrlFormat
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public UrlFormat()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public virtual string NewsUrl(DateTime dt,int NewsID)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetUrl("News,{0},{1},{2}," + NewsID + ".aspx",dt.Year,dt.Month,dt.Day);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected virtual string GetUrl(string pattern, params object[] items)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return string.Format(pattern,items);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
格式化为:
news,2004,8,13,123.aspx
指向:
news.aspx?参数=some parameter
作用:可以隐藏真实Url,爽!
相关Dll文件下载:http://bbs.mvpcn.net/PostAttachment.aspx?PostID=405