Urlrewrite 配置資訊寫在另外的檔案

來源:互聯網
上載者:User

標籤: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 配置資訊寫在另外的檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.