为什么 HttpContext.Request.Url 与地址栏中的内容不匹配?
好的,所以我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使您没有进行任何显式 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