Urlrewrite 配置信息写在另外的文件
由于伪静态的配置 太多,如果放在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>
完成。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。