ASP.NET MVC3:System.Web.HttpException:请求在此上下文中不可用
问题描述:
我开发了一个asp.net mvc3应用程序,它在本地主机上运行良好,但是当我使用所有.dll在线托管时,它会给出错误ASP.NET MVC3:System.Web.HttpException:请求在此上下文中不可用
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
我该如何解决它?我已经尝试了很多
感谢
答
是否this申请?服务器显然是IIS7,你的本地主机可能是IIS6。
从链接引用提到:
这个错误是由于在IIS7集成管道 ,使得在Application_Start事件无法请求上下文中的设计更改。 当使用经典模式(在以前的 版本的IIS上运行时的唯一模式)时,尽管Application_Start事件始终打算作为 应用程序中的全局和请求不可知事件,但该请求上下文曾经可用,即使 也可用一生。 尽管如此,由于ASP.NET应用程序总是通过 对应用程序的第一次请求启动,因此通过静态HttpContext.Current字段可以获得请求 上下文。
答
不知道这是否有助于某人.. 但我将应用程序池设置为经典.NET AppPool,并且错误消失。
马丁
答
请在代码中使用的
HttpContext.Current.Response.Redirect("PageName");
代替
Response.Redirect("PageName");
。