ASP.NET MVC3:System.Web.HttpException:请求在此上下文中不可用

时间:2012-07-10 04:07:18

标签: asp.net-mvc asp.net-mvc-3

我开发了一个asp.net mvc3应用程序,它在localhost上运行正常,但是当我在线托管所有.dll时,它会出错

Server Error in '/' Application.

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: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[HttpException (0x80004005): Request is not available in this context]
   System.Web.HttpContext.get_Request() +8828580
   atulitbaldhama.MvcApplication.Application_Start() in D:\2012\atulitbaldhama\atulitbaldhama\atulitbaldhama\Global.asax.cs:66

[HttpException (0x80004005): Request is not available in this context]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9093225
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +131
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +194
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +339
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +253

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

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

我该如何解决?我已经尝试了很多次

由于

3 个答案:

答案 0 :(得分:1)

this适用吗?服务器显然是IIS7,您的localhost可能是IIS6。

从提到的链接引用:

  

此错误是由于IIS7集成管道中的设计更改造成的   这使得Application_Start事件中的请求上下文不可用。   使用经典模式时(在以前运行时的唯一模式)   IIS的版本,甚至可用的请求上下文   尽管Application_Start事件始终是作为一个   应用程序生命周期中的全局和请求不可知事件。   尽管如此,因为ASP.NET应用程序始终是由   对应用程序的第一次请求,过去可以获得请求   上下文通过静态HttpContext.Current字段。

答案 1 :(得分:1)

不确定这是否有助于某人...... 但我将应用程序池设置为Classic .NET AppPool,错误消失了。

马丁

答案 2 :(得分:0)

请使用

 HttpContext.Current.Response.Redirect("PageName");

而不是

Response.Redirect("PageName");

代码。