为 Redirecting/Forwarding 配置 web.config 一个具有特定模式的 URL 到另一个模式(带冒号和双斜杠)

Configure web.config for Redirecting/Forwarding an URL with specific pattern to another pattern (with colon and double slash)

我有一个旧数据库,其中 returns 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 中尝试了这个,但我无法使匹配 url 工作:

              <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>

会不会是://部分导致无法进行这样的转发?冒号 (:) 导致错误 "potential dangerous request"。身份证能做什么?

最后我得到它来处理这个:

<?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.) 将 requestPathInvalidCharacters 设置为“” 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 中它一直有效)