protected void Application_Start() { RegisterRoutes(RouteTable.Routes); this.BeginRequest += new EventHandler(MvcApplication_BeginRequest); this.EndRequest += new EventHandler(MvcApplication_EndRequest); } void MvcApplication_EndRequest(object sender,EventArgs e) { } void MvcApplication_BeginRequest(object sender,EventArgs e) { }
我可以通过创建默认的ASP.NET MVC应用程序并添加上述代码来重现问题.奇怪的是,这个代码在我的旧主机上工作正常,它只会在我的新(共享)主机上崩溃.如果我的代码中有这些事件处理程序,我得到这个错误:
Server Error in ‘/’ Application.
Object reference not set to an
instance of an object. 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.NullReferenceException: Object
reference not set to an instance of an
object.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:
[NullReferenceException: Object
reference not set to an instance of an
object.]
System.Web.PipelineModuleStepContainer.GetStepArray(RequestNotification
notification,Boolean isPostEvent) +27
System.Web.PipelineModuleStepContainer.GetEventCount(RequestNotification
notification,Boolean isPostEvent) +11
System.Web.PipelineStepManager.ResumeSteps(Exception
error) +205
System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext
context,AsyncCallback cb) +91
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest
wr,HttpContext context) +514
我尝试使用Softsys进行故障排除,但是它们并不是很有帮助,基本上他们只是确认我已经在我的管理控制面板中打开了“ASP.NET管道(MVC)”功能.
有人可以
解决方法
所以你的代码应该是这样的:
public class MySmartApp: HttpApplication{ public override void Init(){ this.BeginRequest += new EventHandler(MvcApplication_BeginRequest); this.EndRequest += new EventHandler(MvcApplication_EndRequest); } protected void Application_Start(){ RegisterRoutes(RouteTable.Routes); } }
或者这样:
public class MySmartApp: HttpApplication{ public MySmartApp(){ this.BeginRequest += new EventHandler(MvcApplication_BeginRequest); this.EndRequest += new EventHandler(MvcApplication_EndRequest); } protected void Application_Start(){ RegisterRoutes(RouteTable.Routes); } }