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

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

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>

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

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

  1. ASP.NET Core开发-读取配置文件Configuration

    ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...

  2. ASP.NET Core开发-读取配置文件Configuration appsettings.json

    https://www.cnblogs.com/linezero/p/Configuration.html ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配 ...

  3. SQL2005SP4补丁安装时错误: -2146233087 MSDTC 无法读取配置信息。。。错误代码1603的解决办法

    是在安装slq2005sp3和sp4补丁的时候碰到的问题. 起先是碰到的错误1603的问题,但网上搜索的1603的解决办法都试过了,google也用了,外文论坛也读了,依然没有能解决这个问题. 其实一 ...

  4. Win7下VS2010使用“ASP.Net 3.5 Claims-aware Template”创建ClaimsAwareWebSite报"HRESULT: 0x80041FEB"错误的解决办法

    问题描述: 使用VS2010的WIF开发模板创建“Claims-aware ASP.NET Site”.“Claims-aware WCF Service”,下载安装后,创建网站时,报错"H ...

  5. python使用xlrd读取excel数据时,整数变小数的解决办法

    python使用xlrd读取excel数据时,整数变小数: 解决方法: 1.有个比较简单的就是在数字和日期的单元格内容前加上一个英文的逗号即可.如果数据比较多,也可以批量加英文逗号的前缀(网上都有方法 ...

  6. 在ASP.NET 5中读取配置文件

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 在ASP.NET 5中摒弃了之前配置文件的基础结构,引入了一个全新配置文件系统.今天推荐的文 ...

  7. asp.net core mvc 读取配置文件appsettings.json

    上一篇我们将了读取自定义配置文件.这篇我们讲一下asp.net core mvc里读取自带的配置文件 appsettings.json 首先创建个asp.net core mvc项目,项目里有Prog ...

  8. asp.net core-6.Bind读取配置文件到C#实例中

    1,创建asp.net core web应用程序,选择空网站 2,创建一个appsettings.json文件 为什么要叫appsettings.json呢?因为在Iwebhost启动的时候没有添加任 ...

  9. C#读取Excel,DataTable取值为空的解决办法

    连接字符串这么些就行了 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + opnFileName ...

随机推荐

  1. 关于ANSI 和 Unicode

    关于ANSI和Unicode 1.ANSI American National Standards Institute(美国国家标准学会),ANSI编码不是一种具体的编码方式,而是一种指定在某些环境下 ...

  2. Android实例-监测网络状态及一些事件(XE8+小米2)

    结果: 1.网络连接:是指现在可不可以上网(你非要问我什么是网,我会K你呀的). 2.WIFI网络:是指现在可以上网,用的是不是WIFI网络(如果你打开了WIFI那它会显示正在使用WIFI). 3.移 ...

  3. uml 在需求分析阶段的应用

    上一篇博客写了uml在软件开发过程中的应用,这以篇要详细介绍一下UML在需求分析过程中的应用. 以机房收费系统为例进行讲解,先介绍一个该系统. 首先该系统的用户分为三个等级,一般用户,操作员,管理员, ...

  4. paip.云计算以及分布式计算的区别

    paip.云计算以及分布式计算的区别 云计算的特点 1 网格计算 2 分布式计算 2 云计算以及网格计算以及分布式计算的区别 2 作者Attilax  艾龙,  EMAIL:1466519819@qq ...

  5. 关于Windows文件名和路径名的那些事

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:关于Windows文件名和路径名的那些事.

  6. MySql实现远程连接

    MySql实现远程连接 1.进入mysql,创建一个新用户root,密码为root: 格式:grant 权限 on 数据库名.表名 to 用户@登录主机 identified by "用户密 ...

  7. 如何用 iptables 禁止某个ip?

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  8. 教你50招提升ASP.NET性能(九):显式的使用using语句减少内存泄露

    (15)Reduce memory leaks dramatically with the “using” statement 招数15: 显式的使用using语句减少内存泄露 If a type i ...

  9. 60款开源云应用【Part 2】(60 Open Source Apps You Can Use in the Cloud)

    60款开源云应用[Part 2](60 Open Source Apps You Can Use in the Cloud) 本篇翻译自http://www.datamation.com/open-s ...

  10. 【每日一摩斯】-【序列】-续-RAC and Sequences (853652.1)

    一个简单的sequence有什么可以说的呢?如果再这样认为就大错特错了... 也许以下几点高人们都很清楚,但至少对于我来说,之前是陌生的,或者说是忽略的. 1.CREATE SEQUENCE seq; ...