码迷,mamicode.com
首页 > Web开发 > 详细

Urlrewrite 配置信息写在另外的文件

时间:2014-10-15 23:49:51      阅读:279      评论:0      收藏:0      [点我收藏+]

标签: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 配置信息写在另外的文件

标签:des   style   blog   http   color   io   ar   for   strong   

原文地址:http://www.cnblogs.com/hougelou/p/4027392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!