错误:HttpContext.Current.Request.ServerVariables

时间:2012-01-09 19:23:17

标签: asp.net

我的serverName和serverPort行都给了我错误... 当我取出servername赋值行时,它在serverport赋值行中给出了错误。唯一常见的是HttpContext.Current.Request ... 我究竟做错了什么?我可以做些什么来解决这种情况?

代码:

serverName = "localhost";
            if (serverName == null || serverName.Equals("localhost"))
                serverName = HttpContext.Current.Server.MachineName.ToLower();
            serverPort = Convert.ToInt32(HttpContext.Current.Request.ServerVariables["SERVER_PORT"]);
            baseUrl = "http://" + serverName;
            if (serverPort != 80)
                baseUrl += ":" + serverPort.ToString();

以下是我尝试调试时给出的错误:

Request is not available in this context
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Request is not available in this context

Source Error:

Line 321:            // Old code left in for if this fails...
Line 322:
Line 323:            serverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
Line 324:            if (serverName == null || serverName.Equals("localhost"))
Line 325:                serverName = HttpContext.Current.Server.MachineName.ToLower();

//************* second source error when i remove serverName line ********/

Source Error:

Line 325:            if (serverName == null || serverName.Equals("localhost"))
Line 326:                serverName = HttpContext.Current.Server.MachineName.ToLower();
Line 327:            serverPort = Convert.ToInt32(HttpContext.Current.Request.ServerVariables["SERVER_PORT"]);
Line 328:            baseUrl = "http://" + serverName;
Line 329:            if (serverPort != 80)

//******************************************///

Source File: C:\CFG.cs    Line: 323

Stack Trace:

[HttpException (0x80004005): Request is not available in this context]
   System.Web.HttpContext.get_Request() +8806688
   labs.shared.config.CFGConstants.InitializeFromHttpContext() in C:\Users\...
   labs.shared.config.CFGConstants..ctor() in C:\Users\...
   labs.shared.config.CFGConstants.Get() in C:\Users\...
   labs.site.framework.FWStateHelper.OnApplicationStart(HttpApplication app) in C:\Users...
   labs.site.Global.Application_Start(Object sender, EventArgs e) in C:\Users\...

[HttpException (0x80004005): Request is not available in this context]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +2724290
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +128
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +188
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +295
   System.Web.HttpApplicationFactory.GetPipelineApplicationInstance(IntPtr appContext, HttpContext context) +56
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +231

[HttpException (0x80004005): Request is not available in this context]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8909915
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +85
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +333

3 个答案:

答案 0 :(得分:0)

你在哪里运行这段代码?如果它在Application_Start中,并且您正在使用IIS7的集成管道,那么您将遇到此问题。

Here's a great article进行更深入的探索。您的选择是:

  1. 不要在Application_Start(推荐)
  2. 中执行此操作
  3. 使用经典管道(不推荐)。

答案 1 :(得分:0)

答案 2 :(得分:0)

正如其他人所指出的那样,当您处于Application_Start时(或者可能是global.asax中的任何其他方法),您没有当前的HttpRequest上下文。

要访问Request对象,您实际上必须等待用户发起请求。您知道,就像他们点击您的目标网页一样。