【问题标题】:ASP.NET trailing slash issue in URL RewrtingURL 重写中的 ASP.NET 尾部斜杠问题
【发布时间】:2012-03-31 14:50:05
【问题描述】:

我正在使用Intelligencia UrlRewriter 进行 URL 重写,我遇到的唯一问题是斜杠。 我在web.config 中有这条规则

<rewrite url="~/moviesarchive/(.+)" to="~/MoviesArchive.aspx?p=$1" />

当我使用http://localhost/site/moviesarchive/1 时它有效,我处理了p 参数为空或空的情况http://localhost/site/MoviesArchive.aspx,但是当我删除尾部斜杠http://localhost/site/moviesarchive 或我使用http://localhost/site/moviesarchive/ 时,我得到@ 987654329@ 错误。我该如何解决?

【问题讨论】:

    标签: asp.net iis-7 url-rewriting


    【解决方案1】:

    【讨论】:

    • 使用&lt;if&gt; 如何检测斜杠是否存在?
    • 使用正则表达式来执行你的两个条件:localhost/site/(moviearchives|moviearchives/)$"> localhost/site/MoviesArchive.aspx" />
    • 我打字速度很快,但在上一条评论中忽略了一个错字。我确定这很明显,但网址不应该以“localhost”开头。改用“~/”
    【解决方案2】:

    在您的页面加载事件中添加:

        Dim rawUrl As String = HttpContext.Current.ApplicationInstance.Request.RawUrl
        If Not rawUrl.EndsWith("/") Then
            HttpContext.Current.ApplicationInstance.Response.RedirectPermanent(String.Format("~{0}/", rawUrl))
        End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多