標籤:des style blog http color io ar for strong
由於偽靜態配置 太多,如果放在web.cofig裡面可閱讀性不強,而且頻繁修改webconfig容易出錯。
1.修改RewriterConfigSerializerSectionHandler類
using System;using System.Configuration;using System.Xml;using System.Xml.Serialization;using System.Xml.XPath;namespace Utility.URLRewriter{ /// <summary> /// Deserializes the markup in Web.config into an instance of the <see cref="RewriterConfiguration"/> class. /// </summary> public class RewriterConfigSerializerSectionHandler : IConfigurationSectionHandler { /// <summary> /// Creates an instance of the <see cref="RewriterConfiguration"/> class. /// </summary> /// <remarks>Uses XML Serialization to deserialize the XML in the Web.config file into an /// <see cref="RewriterConfiguration"/> instance.</remarks> /// <returns>An instance of the <see cref="RewriterConfiguration"/> class.</returns> public object Create(object parent, object configContext, System.Xml.XmlNode section) { string sourcePath = section.Attributes["ConfigSource"].Value; // Create an instance of XmlSerializer based on the RewriterConfiguration type... XmlSerializer ser = new XmlSerializer(typeof(RewriterConfiguration)); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath(sourcePath)); // Return the Deserialized object from the Web.config XML return ser.Deserialize(new XmlNodeReader(xmlDoc)); //// Create an instance of XmlSerializer based on the RewriterConfiguration type... //XmlSerializer ser = new XmlSerializer(typeof(RewriterConfiguration)); //// Return the Deserialized object from the Web.config XML //return ser.Deserialize(new XmlNodeReader(section)); } }}
2.修改web.config
<configSections> <section name="RewriterConfig" type="URLRewriter.Config.CustomRewriterConfigSerializerSectionHandler, URLRewriter" /></configSections><RewriterConfig ConfigSource="/config/URLRewriter.config"></RewriterConfig>
3.添加urlRewriter.config設定檔
<?xml version="1.0"?> <RewriterConfig> <Rules> <!-- Rules for Blog Content Displayer --> <RewriterRule> <LookFor>~/1.aspx</LookFor> <SendTo>~/Default.aspx</SendTo> </RewriterRule> <RewriterRule> <LookFor>~/(\d{4})/(\d{2})/(\d{2})\.aspx</LookFor> <SendTo>~/ShowBlogContent.aspx?year=$1&month=$2&day=$3</SendTo> </RewriterRule> </Rules> </RewriterConfig>
完成。
Urlrewrite 配置資訊寫在另外的檔案