ASP.NET伪静态-无法读取配置文件,因为它超过了最大文件大小的解决办法_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > ASP.NET伪静态-无法读取配置文件,因为它超过了最大文件大小的解决办法

ASP.NET伪静态-无法读取配置文件,因为它超过了最大文件大小的解决办法

 2013/11/4 12:35:47  做饭不洗碗  博客园  我要评论(0)
  • 摘要:一直都在使用微软URLRewriter,具体的使用方法我就不多说了,网上文章很多。但最近遇到一个问题,就是当web.config文件里面设置伪静态规则过多,大于2M的时候,就报错:无法读取配置文件,因为它超过了最大文件大小的解决办法,如图:URLRewriting所有的映射规则都是保存在web.config里面的,导致这个文件过大,所以最好的解决办法就是把里面的映射规则保存到另外一个文件中去。1、下载和安装MSDNURLRewriting.msi地址http://download
  • 标签:.net ASP.NET 解决办法 配置文件 解决 文件 net 配置

一直都在使用微软URLRewriter,具体的使用方法我就不多说了,网上文章很多。

但最近遇到一个问题,就是当web.config文件里面设置伪静态规则过多,大于2M的时候,就报错:无法读取配置文件,因为它超过了最大文件大小的解决办法,如图:

QQ图片20131104102637" border="0" alt="QQ图片20131104102637" src="/Upload/Images/2013110412/FB85660FC73860E8.jpg" width="901" height="480" />

URLRewriting所有的映射规则都是保存在web.config里面的,导致这个文件过大,所以最好的解决办法就是把里面的映射规则保存到另外一个文件中去。

1、下载和安装MSDNURLRewriting.msi地址

http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi

2、打开源码 Config/RewriterConfiguration.cs 找到下面代码,

/// <summary>
/// GetConfig() returns an instance of the <b>RewriterConfiguration</b> class with the values populated from
/// the Web.config file.  It uses XML deserialization to convert the XML structure in Web.config into
/// a <b>RewriterConfiguration</b> instance.
/// </summary>
/// <returns>A <see cref="RewriterConfiguration"/> instance.</returns>
public static RewriterConfiguration GetConfig()
{
    if (HttpContext.Current.Cache["RewriterConfig"] == null)
        HttpContext.Current.Cache.Insert("RewriterConfig", ConfigurationSettings.GetConfig("RewriterConfig"));

    return (RewriterConfiguration) HttpContext.Current.Cache["RewriterConfig"];
}

修改为

/// <summary>
/// GetConfig() returns an instance of the <b>RewriterConfiguration</b> class with the values populated from
/// the Web.config file.  It uses XML deserialization to convert the XML structure in Web.config into
/// a <b>RewriterConfiguration</b> instance.
/// </summary>
/// <returns>A <see cref="RewriterConfiguration"/> instance.</returns>
public static RewriterConfiguration GetConfig()
{
    if (HttpContext.Current.Cache["RewriterConfig"] == null)
    {
        object obj = Acexe.Common.APIXMLSerializer.DeSerializeFromFile(HttpContext.Current.Server.MapPath("~/URLRewriter.config"), typeof(RewriterConfiguration));
        HttpContext.Current.Cache.Insert("RewriterConfig", obj);
    }

    return (RewriterConfiguration)HttpContext.Current.Cache["RewriterConfig"];
}

其中,反序列化类:

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Acexe.Common
{
    public class APIXMLSerializer
    {
        public static object DeSerialize(string xml, Type type)
        {
            return DeSerialize(xml, type, Encoding.UTF8);
        }

        public static object DeSerialize(string xml, Type type, Encoding encode)
        {
            return DeSerialize(xml, type, encode, false);
        }

        public static object DeSerialize(string xml, Type type, Encoding encode, bool needException)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(type);
                MemoryStream stream = new MemoryStream(encode.GetBytes(xml));
                object obj2 = serializer.Deserialize(stream);
                stream.Close();
                stream.Dispose();
                return obj2;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static object DeSerializeFromFile(string filepath, Type type)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(type);
                FileStream stream = new FileStream(filepath, FileMode.Open);
                object obj2 = serializer.Deserialize(stream);
                stream.Close();
                stream.Dispose();
                return obj2;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static object DeSerializeUTF8(string xml, Type type)
        {
            return DeSerialize(xml, type, Encoding.UTF8);
        }

        public static string Serialize(object ob)
        {
            return Serialize(ob, ob.GetType(), Encoding.UTF8);
        }

        public static string Serialize(object ob, Type type)
        {
            return Serialize(ob, type, Encoding.UTF8);
        }

        public static string Serialize(object ob, Type type, Encoding encode)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(type);
                XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                namespaces.Add(string.Empty, string.Empty);
                XmlWriterSettings settings = new XmlWriterSettings();
                MemoryStream output = new MemoryStream();
                settings.OmitXmlDeclaration = true;
                XmlWriter xmlWriter = XmlWriter.Create(output, settings);
                serializer.Serialize(xmlWriter, ob, namespaces);
                xmlWriter.Flush();
                xmlWriter.Close();
                string str = encode.GetString(output.GetBuffer());
                output.Close();
                output.Dispose();
                return str.TrimEnd(new char[1]);
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static void SerializeToFile(object ob, Type type, string filepath)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(type);
                StreamWriter writer = new StreamWriter(filepath);
                serializer.Serialize((TextWriter)writer, ob);
                writer.Close();
                writer.Dispose();
            }
            catch (Exception)
            {
            }
        }

        public static string SerializeUTF8(object ob, Type type)
        {
            return Serialize(ob, type, Encoding.UTF8);
        }
    }
}

3、配置伪静态规则到网站根目录下URLRewriter.config(路径根据实际需要可调整)

<?xml version="1.0"?>
<RewriterConfig>
  <Rules>
    <!--首页-->
    <RewriterRule>
      <LookFor>~/index.html</LookFor>
      <SendTo>~/default.aspx</SendTo>
    </RewriterRule>
    <RewriterRule>
      <LookFor>~/Vacations/(.[0-9]*)S(.[0-9]*)A(.[0-9]*)C(.[0-9]*)D(.[0-9]*)E(.[0-9]*)F(.[0-9]*)H(.[0-9]*)J(.[0-9]*)R(.[0-9]*)T(.[0-9]*)W(.[0-9]*)X(.[0-9]*)Y(.[0-9]*)P(.[0-9]*)/index.html</LookFor>
      <SendTo>~/Vacations/List.aspx?sid=$1&amp;A=$3&amp;C=$4&amp;D=$5&amp;E=$6&amp;F=$7&amp;H=$8&amp;J=$9&amp;R=$10&amp;T=$11&amp;W=$12&amp;X=$13&amp;Y=$14&amp;P=$15</SendTo>
    </RewriterRule>
  </Rules>
</RewriterConfig>

4、配置Web.Config文件(和网上其他说明的配置一样)

     (1)添加下面内容到configSections节点下

<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>

     (2)添加下面内容到httpHandlers节点下

<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>

     (3)如果是IIS7,添加下面内容到handlers节点下

<!--IIS7URL重写配置开始-->
<add name="all" path="*" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv2.0,bitness64" />
<add name="Html" path="*.html" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness64" />
<add name="ASPNET_ISAPI" path="*" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
<!--IIS7URL重写配置结束—>

以下是全部Web.Config文件配置

<?xml version="1.0"?>
<!-- 
    注意: 除了手动编辑此文件外,您还可以使用 
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
    “网站”->“Asp.Net 配置”选项。
    设置和注释的完整列表可以在
    machine.config.comments 中找到,该文件通常位于
      \Windows\Microsoft.Net\Framework\vx.x\Config 中 
-->
<configuration>
  <configSections>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
          <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
        </sectionGroup>
      </sectionGroup>
    </sectionGroup>
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
  </configSections>


  <system.web>
    <!-- 
            设置 compilation debug="true" 可将调试符号插入到
            已编译的页面。由于这会
            影响性能,因此请仅在开发过程中将此值
            设置为 true。
        -->
    <compilation debug="true">
      <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
      <buildProviders>
        <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
      </buildProviders>
    </compilation>
    <!--
            通过 <authentication> 节可以配置
            安全身份验证模式,ASP.NET 
            使用该模式来识别来访用户身份。 
        -->
    <authentication mode="Windows"/>
    <!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节
            可以配置相应的处理步骤。具体而言,
            开发人员通过该节可配置要显示的 html 错误页,
            以代替错误堆栈跟踪。

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    <pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </controls>
    </pages>
    <httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>

      <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
      <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
    </httpHandlers>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5"/>
        <providerOption name="OptionInfer" value="true"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
    </compilers>
  </system.codedom>
  <!-- 
        system.webServer 节是在 Internet Information Services 7.0 下运行 ASP.NET AJAX
        所必需的。对早期版本的 IIS 来说则不需要此节。
    -->
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
      <remove name="ScriptModule"/>
      <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </modules>
    <handlers>
      <remove name="WebServiceHandlerFactory-Integrated"/>
      <remove name="ScriptHandlerFactory"/>
      <remove name="ScriptHandlerFactoryAppServices"/>
      <remove name="ScriptResource"/>
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

      <!--IIS7URL重写配置开始-->
      <add name="all" path="*" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv2.0,bitness64" />
      <add name="Html" path="*.html" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness64" />
      <add name="ASPNET_ISAPI" path="*" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
      <!--IIS7URL重写配置结束-->
    </handlers>
    <defaultDocument>
      <files>
        <clear />
        <add value="default.aspx" />
        <add value="index.html" />
        <add value="index.asp" />
      </files>
    </defaultDocument>
  </system.webServer>
  <runtime>
    <assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

qrcode

三五旅游网 http://www.35lvyou.com

发表评论
用户名: 匿名