删除url尾部正斜杠ASP.NET Webforms,IIS7

时间:2015-07-15 04:09:55

标签: c# webforms iis-7

我正在维护现有的ASP.NET Webforms应用程序。出现了一个问题,我们现在需要保证从所有URL的

中删除尾部斜杠
//! The ini namespace
namespace ini {
...
}

现在这是一个已存在多年的应用程序,并且URL是在代码后面直接构建的,直接在aspx文件中构建 - 基本上无处不在。添加到混合中的是,虽然ASPX应用程序配置为使用 https://www.example.com/blah.aspx --> https://www.example.com/blah.aspx https://www.example.com/blah --> https://www.example.com/blah https://www.example.com/blah/ --> https://www.example.com/blah https://www.example.com/blah/?a=1 --> https://www.example.com/blah?a=1 进行路由。所以我们也可能有:

在定义中没有尾随斜杠

System.Web.Routing

那么解决方案似乎是我需要在IIS中添加重写规则,还是应该在 aRoutes.MapPageRoute("routeBrandsCamp", "brand/{name}/camp", "~/Pages/Brand/Camp.aspx", true, new RouteValueDictionary { {"name", " "} }); aRoutes.MapPageRoute("routeBrandsSummary", "brand/{name}/summary/", "~/Pages/Brand/Summary.aspx", true, new RouteValueDictionary { {"name", " "} }); 中执行某些操作?

无论解决方案是什么,您还可以提供一个有效的例子吗?例如。 IIS重写规则,一些代码等。

基本上我想在一个地方改变它而不破坏应用程序; - )

1 个答案:

答案 0 :(得分:2)

IIS重写模块在system.webServer部分下保留一个节点,并允许我们很容易地在那里配置设置。

system.webServer node:

下添加以下代码
<rewrite>
  <rules>

    <!--To always remove trailing slash from the URL-->
    <rule name="Remove trailing slash" stopProcessing="true">
      <match url="(.*)/$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="{R:1}" />
    </rule>

  </rules>
</rewrite>

更多参考herecreating rewrite modules