加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 为什么我的主机(softsyshosting.com)不支持BeginRequ

发布时间:2020-12-15 19:43:42 所属栏目:asp.Net 来源:网络整理
导读:我听说过Softsys Hosting的好东西,所以我决定把我的ASP.NET MVC解决方案移交给他们.但它不会运行在他们身上.我能够将问题精确到我的BeginRequest事件处理程序.如果我有他们,我会得到一个错误.这是我的代码 protected void Application_Start(){ RegisterRout
我听说过Softsys Hosting的好东西,所以我决定把我的ASP.NET MVC解决方案移交给他们.但它不会运行在他们身上.我能够将问题精确到我的BeginRequest事件处理程序.如果我有他们,我会得到一个错误.这是我的代码
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)”功能.

有人可以

告诉我,如果我编码错了
给我一个解决办法
>向我解释为什么这个错误发生在一个主机而不是其他主机上.

解决方法

您需要在每个HttpApplication实例中注册您的处理程序.可能有几个HttpApplication的池化实例. Application_Start只能在任何请求之前调用一次(对于IIS 6和IIS 7,在经典模式下 – 在第一个请求中为IIS 7集成模式 – 在Web应用程序启动时).所以为了让所有的工作,你需要添加事件处理程序在HttpApplication的重写Init方法或其构造函数.如果您在构造函数中添加它们,那么这些处理程序将首先被调用,甚至在注册模块的处理程序之前被调用.
所以你的代码应该是这样的:
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);
    } 
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读