How to get an 'MvcApplication' instance in ASP.NET controller?

后端 未结 3 1582
夕颜
夕颜 2021-02-02 10:52

I think MvcApplication is a global singleton. I want to get the instance of MvcApplication in the controller. Then I put the following code in controll

3条回答
  •  不知归路
    2021-02-02 11:32

    I believe the reason why the original code didn't work is because HttpContext is both a property of Controller and its own class. Within a subclass of Controller, HttpContext will resolve to the property and produce the error mentioned. To get around it, explicitly reference the HttpContext class with it's fully qualified name:

    System.Web.HttpContext.Current.Application
    

    Or, since the HttpContext property already returns the current HttpContext instance, you could use:

    HttpContext.Application
    

提交回复
热议问题