【问题标题】:Incorrect redirect from ASP.NET MVC application under URL Rewrite在 URL 重写下从 ASP.NET MVC 应用程序重定向不正确
【发布时间】:2015-04-16 05:39:47
【问题描述】:

我在 IIS 中有网站的下一个配置:

  • http://main.domain.com网站(ASP.NET MVC 5 app,网站绑定到指定主机,80端口)
  • 默认网站绑定到 :80 并启用 ARR/URL 重写模块

在 DNS 设置中指定了通配符绑定 *.domain.com

期望的行为是将http://main.domain.com 作为入口点和一组动态用户子域,例如http://user1.domain.comhttp://user2.domain.com 等。

现在这种行为是使用http://main.domain.com/user/user1之类的链接来模拟的

我已经为main.domain.com 设置了 URL 重写规则,如下所示:

<rule name="user-redirection" enabled="true" stopProcessing="true">
  <match url="^.*$" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
    <add input="{HTTP_HOST}" pattern="^([\w\d-]+)\.domain\.com$" />
  </conditions>
  <action type="Rewrite" url="http://main.domain.com/user/{C:1}/{R:0}" logRewrittenUrl="true" />
</rule>

这里一切正常 - 我可以看到 http://user1.domain.com 的工作方式与之前的 http://main.domain.com/user/user1 一样。

然后我尝试重新实现检查数据库中指定用户是否存在的逻辑。例如,当 user47 不存在时 - 打开http://main.domain.com/user/user47 链接会导致重定向到http://main.domain.com 入口点。

在代码端,它是通过将自定义过滤器属性添加到实现所需条件重定向的控制器操作来完成的。我有下一个代码:

public class UserController : Controller {
    [CustomRedirectBehavior]
    public ActionResult Index()
    {
       ...
    }
}

public class CustomRedirectBehaviorAttribute : ActionFilterAttribute {
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        ...

        if (redirectingCondition) {
          filterContext.Result = new RedirectResult("http://main.domain.com");
        }
    }
}

这里....我在浏览器中收到循环重定向错误!可以肯定的是 - 我已经仔细检查了这种行为:

  1. 当我打开http://main.domain.com/user/user47 时,我被正确重定向到http://main.domain.com
  2. 当我打开 http://user47.domain.com/ 时出现循环重定向错误。

然后为了调查问题,我将重定向回调修改为:

filterContext.Result = new RedirectResult("http://someotherdomain.com/some/other/path");

而且....我可以看到:

  1. 当我打开http://main.domain.com/user/user47 时,我被正确重定向到http://someotherdomain.com/some/other/path
  2. 当我打开http://user47.domain.com/ 时,我被重定向到http://user47.domain.com/some/other/path !!! (是的,这不是错字,我也仔细检查了这种行为)

所以。我需要一个关于如何解决这个问题的想法

【问题讨论】:

    标签: asp.net-mvc iis url-rewriting arr


    【解决方案1】:

    我试图重现您的情况,看来您确实遇到了重定向循环。所以我在模式中添加了一个负前瞻组 ((?!main)),它对我有用。这就是我的 web.config 的样子:

    <rule name="user-redirection" enabled="true" stopProcessing="true">
        <match url="^.*$" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
            <add input="{HTTP_HOST}" pattern="^(?!main)([\w\d-]+)\.domain\.com$" />
        </conditions>
        <action type="Rewrite" url="http://main.domain.com/user/{C:1}" logRewrittenUrl="true" />
    </rule>
    

    为了调查您的问题,我已经尝试过:

    1. 禁用浏览器缓存。如果您的浏览器使用缓存,您可能不会看到您对规则所做的所有更改。
    2. 将操作类型更改为“重定向”而不是“重写”,并在“开发人员工具 (F12)”的“网络”选项卡中观察发生的情况。 例如&lt;action type="Redirect" ...

    【讨论】:

    • 很好的答案。需要一些时间来检查它是否有帮助。您有什么想法吗,为什么我在最后指定的情况下将http://user47.domain.com/some/other/path 作为目标 URL?
    • 我已经尝试了几次,它对我有用。您可以看到 RedirectResult 实际为您做了什么here。您也可以尝试使用System.Web.HttpContext.Current.Response.Redirect("http://domain.com") 手动执行此操作。您使用的是哪个版本的 asp.net mvc?您还有其他重定向/重写吗?您是否有任何代码可以清除响应中的“重定向”标头?
    • 感谢您的大力帮助。我刚刚找到答案并发布在下面。
    【解决方案2】:

    嗯……我终于得到了答案。

    问题来自正在安装的应用程序请求路由 (ARR) 模块,以便使用自定义子域进行重写。

    服务器代理设置区域启用代理后,Reverse rewrite host in response headers 选项默认设置为 true。禁用此设置可使重定向在基于子域的重写下正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 2021-05-19
      • 2011-02-21
      • 2017-12-12
      • 2011-10-31
      • 2014-03-07
      • 1970-01-01
      相关资源
      最近更新 更多