为什么 HttpContext.Request.Url 与地址栏中的内容不匹配?

发布于 2024-07-29 20:00:38 字数 1265 浏览 5 评论 0原文

好的,所以我想在 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);
    }
}

Ok, so I want to force https/ssl on parts of my ASP.NET MVC site. Found lots of great sources including an ActionFilter here: http://blog.tylergarlick.com/index.php/2009/07/mvc-redirect-to-http-and-https/

Whenever I enable the ActionFilter however I end up in a redirect loop. The problem is that if I type https://www.mysite.com/ into the address bar, request.url is always equal to http://www.mysite.com/.

The code for the action filter is below and to my knowledge I'm not doing any url rewriting, redirecting, custom routing or modifying the url beyond the standard setup. Are there any common/uncommon reasons why this might be happening and/or any workarounds or fixes? The site is currently hosted at NetworkSolutions - is there a chance it's related to IIS configuration? Any assistance would be much appreciated.

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生未歇 2024-08-05 20:00:39

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

Even though you're not doing any explicit URL rewriting, it is done by the ASP.NET MVC engine.
You can probably retrieve the original URL with HttpContext.Request.RawUrl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文