【问题标题】:Dot at end of URL using MVC4 throws error使用 MVC4 在 URL 末尾的点会引发错误
【发布时间】:2016-06-06 17:20:55
【问题描述】:

http://localhost:56472/test. 抛出丑陋的黄色 ASP.NET 错误消息 - The Resource Cannot be Found

我如何捕捉到这个请求和

  1. 防止丑陋的黄色
  2. 从请求中去除句点并重定向?

我的Global.asax 里确实有这个:

protected void Application_Error(object sender, EventArgs e) {
        // Do whatever you want to do with the error
}

但是错误没有被捕获。此处捕获了正常的 404,我成功重定向到自定义错误页面。

我正在部署到 Azure PaaS,因此对 IIS 没有太多精细控制。

更新:我的重写尝试:

    <rule name="Strip Periods" stopProcessing="true">
      <match url="^(.*[^.])(\.+)$" />
      <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^site.com$" />
      </conditions>
      <action type="Redirect" url="{MapProtocol:{HTTPS}}://www.site.com/{R:1}" />
    </rule>

【问题讨论】:

  • 可以通过使用适当的错误页面来防止丑陋的黄色。有一种特定于 MVC 的方法,然后是一种适用于所有 ASP.NET 的方法。至于URL,是不是你的系统生成的URL?还是用户直接在地址栏中输入?还是其他系统生成了指向您网站的错误链接?
  • @mason 我知道这一点,并且 - 如前所述 - 我有自定义错误处理,在这种情况下它正在工作 except。这是有人点击了格式错误的链接,在末尾包含句点。
  • 但是谁创建了链接?你的网站?还是第三者?
  • @mason 有人链接到我的页面并意外包含了句号。
  • How to do it using rewrite module(来自相关问题之一)

标签: asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

您需要两个步骤来执行此操作。首先,处理重定向的重写规则:

<rule name="RemoveTrailingDot" stopProcessing="true">
    <match url="(.*)\.+$" />
    <action type="Redirect" url="{R:1}" />
</rule>

其次,以下指令(在this related answer中找到)

<system.web>
    <httpRuntime relaxedUrlToFileSystemMapping="true"/>
</system.web>

相关答案中的 cmets 表明它并不总是有效,但它在 Windows 10 上的 IIS 上为我工作。

【讨论】:

  • 刚刚测试 - 仍然看到丑陋的黄色:(
  • 得到它的工作有点额外 - 请参阅更新的答案。
猜你喜欢
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 2016-08-29
  • 1970-01-01
相关资源
最近更新 更多