源码:
protected void Application_BeginRequest(object sender, EventArgs e) { HttpApplication application = sender as HttpApplication; HttpContext context = application.Context; HttpRequest request = context.Request; HttpResponse response = context.Response; if (request.Url.Scheme != "https") { Page301Url(response, "https://" + request.Url.Host + request.RawUrl); } } protected void Page301Url(HttpResponse response, string url301) { response.Clear(); response.Cache.SetCacheability(HttpCacheability.NoCache); response.Cache.SetExpires(DateTime.UtcNow.AddYears(-1)); response.Cache.SetMaxAge(TimeSpan.Zero); response.Cache.SetNoServerCaching(); response.Cache.SetNoStore(); response.Cache.SetNoTransforms(); response.Cache.SetProxyMaxAge(TimeSpan.Zero); response.StatusCode = (int)HttpStatusCode.MovedPermanently; response.AddHeader("Location", url301); response.End(); }
在Global里写好逻辑,网站运行的时候会首先进入Global,这里介绍的是http如何跳https.