首页 > 代码库 > Urlrewrite 配置信息写在另外的文件
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>
完成。
Urlrewrite 配置信息写在另外的文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。