在 global.asax 文件中使用上下文时出现问题
我有用户 global.asax 并且我在 ASP.NET 应用程序中编写了这段代码:
<%@ Application CodeBehind="Global.asax.cs" Language="C#" %>
<script RunAt="server">
public void Application_Start()
{
string str = Context.Request.Url.AbsoluteUri.Replace("http", "https");
Context.RewritePath(str);
}
</script>
但它给了我这个:
"Request is not available in this context"
I have user global.asax and I have write this code in my ASP.NET application :
<%@ Application CodeBehind="Global.asax.cs" Language="C#" %>
<script RunAt="server">
public void Application_Start()
{
string str = Context.Request.Url.AbsoluteUri.Replace("http", "https");
Context.RewritePath(str);
}
</script>
but it gives me this :
"Request is not available in this context"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 http 到 https 需要进行重定向。
RewritePath 不起作用的主要原因是 http 和 https 在不同的端口上工作。此外,应用程序启动也不是调用此想法的地方。 BeginRequest 就是其中之一。
因此,如果您想将所有请求自动更改为 https,请使用此代码。
您还可以使用此模块自动进行此切换。
From http to https you need to make redirect.
The main reason the RewritePath is not working is because the http and https work on different ports. Also the Application Start is not the place to call this thinks. The BeginRequest is the one.
So if you like to change all the request automatically to https, use this code.
You can also use this module to make this switching automatically.
请求在
Application_Start
中不可用,为时过早。您需要重写
Application_BeginRequest
中的路径,请参阅 http://msdn.microsoft.com/en-us/library/sa5wkk6d.aspx 为例。Request is not available in
Application_Start
, it's too early.You'll want to rewrite the path in
Application_BeginRequest
, see http://msdn.microsoft.com/en-us/library/sa5wkk6d.aspx for an example.