【问题标题】:Why is HttpContext.Request.Url not matching what's in the address bar?为什么 HttpContext.Request.Url 与地址栏中的内容不匹配?
【发布时间】:2009-07-30 06:24:03
【问题描述】:

好的,所以我想在我的 ASP.NET MVC 站点的某些部分上强制使用 https/ssl。在这里找到了很多很棒的资源,包括 ActionFilter:http://blog.tylergarlick.com/index.php/2009/07/mvc-redirect-to-http-and-https/

每当我启用 ActionFilter 时,我都会进入重定向循环。问题是如果我在地址栏中输入 https://www.mysite.com/,request.url 总是等于 http://www.mysite .com/.

操作过滤器的代码如下,据我所知,我不会在标准设置之外进行任何 url 重写、重定向、自定义路由或修改 url。是否有任何常见/不常见的原因导致这种情况发生和/或任何解决方法或修复?该站点目前托管在 NetworkSolutions - 是否有可能与 IIS 配置有关?任何帮助将不胜感激。

public class RedirectToHttps : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Helpers just so I don’t have to type so much
        var request = filterContext.HttpContext.Request;
        var response = filterContext.HttpContext.Response;

        // Make sure we’re in https or local
        if (!request.IsSecureConnection && !request.IsLocal)
        {
            string redirectUrl = request.Url.ToString().Replace("http:", "https:");
            response.Redirect(redirectUrl);
        }

        base.OnActionExecuting(filterContext);
    }
}

【问题讨论】:

  • 另外,我尝试使用 MVC 期货 RequireSSL 属性并遇到相同的重定向循环

标签: asp.net-mvc http ssl https httpcontext


【解决方案1】:

即使您没有进行任何显式 URL 重写,它也是由 ASP.NET MVC 引擎完成的。 您可能可以使用 HttpContext.Request.RawUrl 检索原始 URL

【讨论】:

  • 感谢您抽出宝贵时间回答我的问题。似乎 RawUrl 只给了我相对路径:例如 /default.aspx。这正常吗?
  • 我认为它包含原始 URL,但根据文档:“原始 URL 被定义为域信息之后的 URL 部分”。对不起...
猜你喜欢
  • 2021-11-05
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 2016-02-03
  • 1970-01-01
  • 2018-10-30
相关资源
最近更新 更多