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

时间:2018-09-04 14:18:40

标签: asp.net iis web-config

我有一个旧数据库,该数据库返回带有链接到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。)将requestPathInvalidCharacters设置为“” 2.) 3.)

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

1 个答案:

答案 0 :(得分:0)

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

<?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中,它一直在工作)