配置 web.config 以将具有特定模式的 URL 重定向/转发到另一种模式(带冒号和双斜杠)



我有一个旧数据库,它返回hughe文本,其中包含指向 ASP.NET 应用程序中不存在的URL的链接。链接如下所示:

http://bbmag295:7777/$element://%7B57980C01-3974-49e5-91D4-49B843359557%7D

并且应该转换为:

http://bbmag295:7777/ShowGlossary.aspx?id=57980C01-3974-49e5-91D4-49B843359557

这意味着应该选择"$element://%7B"和"%7D"之间的模式并将其重定向到ShowGlossary.aspx?id=...

为此,我用 C# 编写了以下 RegMatch-Expression:

string pattern2 = @"(element://{[^>]+})";
MatchCollection matches2 = Regex.Matches(neuerString, pattern2);
if (matches2.Count > 0)
{
foreach (Match m in matches2)
{
string toReplace = "$" + m.Groups[1].ToString();
string guid = toReplace.ToString().Replace("$element://", "");
neuerString = neuerString.Replace(toReplace, "ShowGlossary.aspx?id=" + guid); 
}
}

我刚刚在 Web.Config 中尝试了这个,但我无法让匹配网址正常工作:

<rewrite>
<rules>
<rule name="Query String Rewrite">  
<!--<match url="(element://{[^>]+})" />-->
<!--<match url="^Article/([0-9]+)/([_0-9a-z-]+)" />-->
<!--<action type="Rewrite" url="ShowGlossary.aspx?ID={R:1}"/>  -->
<!--<match url="^Article/([0-9]+)/([_0-9a-z-]+)" />-->
<match url="^$element://([_0-9a-z-]+)" />
<action type="Rewrite" url="ShowGlossary.aspx?id={R:1}" />
</rule>      
</rules>
</rewrite>

是否有可能 ://部分无法进行这样的转发?冒号 (:) 会导致错误为"潜在危险请求"。id能做什么?

最后我让它处理这个:

<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.webServer>
<security> 
<requestFiltering allowHighBitCharacters="true" allowDoubleEscaping="true" /> 
</security>
<rewrite>
<rules>
<rule name="Query String Rewrite">  
<match url="^$element:/{([_0-9a-z-]+)}" />
<action type="Rewrite" url="ShowGlossary.aspx?id={R:1}" />
</rule>      
</rules>
</rewrite>
</system.webServer>
<connectionStrings>
<add name="EAPFile" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=~MDB/plm.eap" providerName="System.Data.SqlClient" />
<add name="plmConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" providerName="System.Data.OleDb" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" requestPathInvalidCharacters=""/>
<pages validateRequest="false" />
</system.web>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>

1.( 将请求路径无效字符设置为 " 2.) 3.(

我的下一个问题是,重写后没有加载样式:'(

最后,我让它使用重定向而不是重写来工作。

<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.webServer>
<security> 
<requestFiltering allowHighBitCharacters="true" allowDoubleEscaping="true" /> 
</security>
<rewrite>
<rules>
<rule name="Query String Rewrite">  
<match url="^$element:/{([_0-9a-z-]+)}" />
<action type="Redirect" url="ShowGlossary.aspx?id={R:1}" />
</rule>      
</rules>
</rewrite>
</system.webServer>
<connectionStrings>
<add name="EAPFile" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=~MDB/plm.eap" providerName="System.Data.SqlClient" />
<add name="plmConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" providerName="System.Data.OleDb" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" requestPathInvalidCharacters=""/>
<pages validateRequest="false" />
</system.web>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>

之后,我必须下载重写模块,否则我在IIS中出现错误500(在Visual Studio中,它已经一直工作了(

相关内容

最新更新