-2

我有以下代码:

      if (Request.Url.AbsolutePath == "/Text12.aspx")

我喜欢做的是我喜欢让http成为

    http://www.mysitename/Text12.aspx 

无论用户是否输入

    www.mysitename/Text12.aspx or 
    https://www.mysitename/Text12.aspx or 
    http://www.mysitename/Text12.aspx
4

1 回答 1

0

我正在尝试回答。鉴于您希望用户始终使用 http。在我的一些应用程序中,我通过以下代码强制执行 https:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.Request.IsSecureConnection.Equals(false) && HttpContext.Current.Request.IsLocal.Equals(false))
    {
      Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"]
      + HttpContext.Current.Request.RawUrl);
    }
}

这也应该反过来工作:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.Request.IsSecureConnection.Equals(true) && HttpContext.Current.Request.IsLocal.Equals(true))
    {
       Response.Redirect("http://" + Request.ServerVariables["HTTP_HOST"]
       + HttpContext.Current.Request.RawUrl);
    }
}

//编辑: 请注意,我已将 Islocal 设置为 true,因此仅在本地调用时才会触发 - 或者在您的开发机器上作为可能的场景。您仍然需要使其适合您的需求,或者完全消除这种情况。

于 2013-09-11T14:25:00.810 回答