使用 C# 中的 Global.asax 文件进行重定向
我已将以下代码添加到我的 Global.asax 文件中:
<%@ Application Language="C#" %>
<script runat="server">
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (ConfigurationManager.AppSettings["IsReviewServer"] == "Yes")
{
if (!Request.IsSecureConnection)
{
string path = string.Format("https{0}", Request.Url.AbsoluteUri.Substring(4));
Response.Redirect(path);
}
}
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
etc.....
但我的 BeginRequest 函数只是被忽略。如何将整个应用程序从 http: 重定向到 https:?
I have added the following code to my Global.asax file:
<%@ Application Language="C#" %>
<script runat="server">
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (ConfigurationManager.AppSettings["IsReviewServer"] == "Yes")
{
if (!Request.IsSecureConnection)
{
string path = string.Format("https{0}", Request.Url.AbsoluteUri.Substring(4));
Response.Redirect(path);
}
}
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
etc.....
But my BeginRequest function just gets ignored. How do I redirect my entire application from http: to https:?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用母版页或基类,我会将您的逻辑放在那里。这样的逻辑不应该依赖于全球事件。
将逻辑放在母版页或基类的
Page_Load
(或生命周期的早期部分)中,如下所示:如果您也愿意,可以在生命周期的另一个点执行上述操作,例如
PreLoad
或预渲染
。使用全局事件
如果您要使用全局事件,我实际上会使用
Application_EndRequest
,因为它会在每个请求上调用,以便应用程序可以清理资源。If you're using a master page or a base class, I would put your logic there. Global events shouldn't be relied upon for logic like this.
Put the logic in
Page_Load
(or earlier in the lifecycle) of the master page or base class like this:You could do the above at another point in the lifecycle if you wanted too, like
PreLoad
orPreRender
.Using global events
If you're going to use a global event, I would actually use
Application_EndRequest
, because it gets called on every request so the application can clean up resources.