URL重写模式以删除网址末尾的尾随点(...)

时间:2016-05-03 22:25:46

标签: asp.net-mvc url-rewriting asp.net-mvc-routing

我的MVC网站有路由问题,当网址末尾添加了三个点时,显示屏显示标准的ASP.Net 404错误,但我希望它路由到我们的自定义错误页面。

' /'中的服务器错误应用

无法找到资源。

描述:HTTP 404.您正在查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用。请查看以下网址,确保拼写正确。

请求的网址:/ ABC /床垫/.../

据我所知,它甚至没有进入MVC。我认为服务器将其视为一个问题,它正在以我们不想要的方式处理它。我不知道如何解决这个问题,以便我可以将此网址路由到我们的自定义错误页面。

我尝试的事情:

routes.MapRoute("MyRoute", "abc/{Keyword}", new { controller = "KeywordSearch", action = "Index", Keyword = UrlParameter.Optional});

routes.MapRoute("MyRouteCatchAll", "abc/{Keyword}/{*CatchAll}", new { controller = "Base", action = "NotFound", Keyword = UrlParameter.Optional, CatchAll = UrlParameter.Optional });

routes.MapRoute("CatchAll", "{*catchAll}", new { controller = "KeywordSearch", action = "NotFound" });

我也尝试过使用网址重写。也许正则表达式是错误的。

<rule name="trailingdots" enabled="true" stopProcessing="true">
      <match url="abc/([_0-9a-z-]+)/^([^.]+)$" />
      <action type="Redirect" url="sr/{R:1}/" />
    </rule>

我的智慧结束了。我无法在任何地方找到任何信息来处理这种情况。

非常感谢任何帮助。

更新:我能够获得URL重写规则

<rule name="RemoveDotsAndSlash" stopProcessing="true">
      <match url="(.*)/.../$" />
      <action type="Rewrite" redirectType="Permanent" url="{R:1}" />
    </rule>
    <rule name="RemoveDots" stopProcessing="true">
      <match url="(.*)/...$" />
      <action type="Rewrite" redirectType="Permanent" url="{R:1}" />
    </rule>

但我希望得到这样的东西,但它仍然需要我到通用的404页面:

<rule name="RemoveTrailingDotsAndSlash" stopProcessing="true">
      <match url="^(.*)$" />
      <conditions logicalGrouping="MatchAny">
        <add input="{REQUEST_URI}" pattern="^(.*)/.../$" />
    <add input="{REQUEST_URI}" pattern="^(.*)/...$" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="{R:1}" />
    </rule>

关于我到底能得到我想要的东西的任何想法?

由于

1 个答案:

答案 0 :(得分:3)

您不需要使用图案删除它。如果将以下标记添加到web.config,则可以使用自定义错误重定向到默认错误页面:

<system.web>    
  <httpRuntime relaxedUrlToFileSystemMapping="true"/> 
  <customErrors mode="On" defaultRedirect="error.aspx">
     <error statusCode="404" redirect="notFound.aspx" />
  </customErrors>
    [ ...]
</system.web>

documentation,中,解释是设置 relaxedUrlToFileSystemMapping = true 相当于允许在URL中使用无效的Windows路径,然后将其重定向到404错误,我们就可以捕获它。