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