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

标签 asp.net-mvc http ssl https httpcontext

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

最佳答案

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

检索原始 URL

关于asp.net-mvc - 为什么 HttpContext.Request.Url 与地址栏中的内容不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1204772/

相关文章:

c# - ASP.NET MVC3 双重验证(逗号、点、空)

用于测试 HTTP 响应代码和时间的 iOS App

ssl - 我如何使用 mbedtls 验证证书是否验证了 key ?

android - 如何使 WebView 每次重新加载页面时显示 SSL 错误?

javascript - jquery 加载对 Controller 的调用并填充 div 元素

javascript - 如果满足条件,则从 Controller 返回 JsonResult 中的模式确认框

asp.net - 使用 Windows 身份验证注销 ASP.NET MVC 应用程序并能够使用同一用户再次登录

等待锁定释放时的 Http Statuscode 需要很长时间?

javascript - 使用 AJAX 和跨站点脚本读取 header

ssl - SSL session 缓存和过期如何工作