一直都在使用微软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. Mysql的函数使用方法

    今天有点临时需求要计算一张表的结果,不想写代码,想到了mysql的自定义函数.碰到了很多问题,为了方便一下使用,在此记录一下. 需求:一张表中,有比分,需要查询出比赛id和比赛结果. 分析:     ...

  2. 【VMware虚拟机】【克隆问题】在VMware 9.0下克隆CentOS6.5虚拟机无法识别eth网卡

    [日期]2014年4月23日 [平台]windows 8 vmware workstation 9.0 centos 6.5 [日志]1)执行ifconfig命令,输出: 2)查看/etc/udev/ ...

  3. setResult()设置无效,onActivityResult没有被调用

    情况1 呃,被坑了几个小时,后来发现,在调用setResult的时候,requestCode随便传了个Activity的RESULT_OK,而这个常量的值是-1,导致onActivityResult没 ...

  4. Android实例-使用电话拨号器在移动设备上(官方)(XE8+小米2)

    源文地址: http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Using_the_Phone_Dialer_on_Mob ...

  5. python 错误处理

    在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因.在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数open(),成功时返回文件描 ...

  6. 新浪新闻API接口

    头条 http://api.sina.cn/sinago/list.json?channel=news_toutiao推荐 http://api.sina.cn/sinago/list.json?ch ...

  7. support-v4不能查看源码

    在Android实际开发过程中往往会遇到使用v4,v7或v13兼容包中的一些类如ViewPager,Fargment等,但却无法关联源码 具体步骤: 1 右击Android项目中libs文件夹下的an ...

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

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

  9. .编写Java应用程序。首先,定义一个Print类,它有一个方法void output(int x),如果x的值是1,在控制台打印出大写的英文字母表;如果x的值是2,在 控制台打印出小写的英文字母表。其次,再定义一个主类——TestClass,在主类 的main方法中创建Print类的对象,使用这个对象调用方法output ()来打印出大 小写英文字母表。

    package com.homework.zw; //类Print部分 public class Print1 { int x; void output() { if(x==1) { for(int ...

  10. Windows 环境下基于 nginx 的本地 PyPI 源

    Windows 环境下基于 nginx 的本地 PyPI 源的搭建: 1.登录 nginx 官网,下载安装包