【问题标题】:Removing url trailing forward slash ASP.NET Webforms, IIS7删除 url 尾部正斜杠 ASP.NET Webforms,IIS7
【发布时间】:2015-07-15 04:09:54
【问题描述】:

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

 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

现在这是一个已经存在多年的应用程序,并且 URL 是在代码后面或直接在 aspx 文件中构建的 - 基本上无处不在。此外,虽然是一个 ASPX 应用程序,但它被配置为使用System.Web.Routing 进行路由。所以我们可能还有:

定义中没有斜杠

 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 中添加一个重写规则,或者我应该在BeginRequest() 中做些什么?

无论解决方案如何,您还可以提供一个可行的示例吗?例如。 IIS重写规则,一些代码等

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

【问题讨论】:

  • 是否要从 URL 末尾删除正斜杠?
  • 是的......你看到我提供的网址列表了吗?

标签: c# webforms iis-7


【解决方案1】:

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

system.webServer节点下添加如下代码:

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

更多参考here,creating rewrite modules

【讨论】:

    猜你喜欢
    • 2017-05-03
    • 1970-01-01
    • 2018-10-19
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多