Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext on instances of Controllers are null in ASP.net MVC

This may not be the correct way to use controllers, but I did notice this problem and hadn't figured out a way to correct it.

public JsonResult SomeControllerAction() {      //The current method has the HttpContext just fine     bool currentIsNotNull = (this.HttpContext == null); //which is false          //creating a new instance of another controller     SomeOtherController controller = new SomeOtherController();     bool isNull = (controller.HttpContext == null); // which is true      //The actual HttpContext is fine in both     bool notNull = (System.Web.HttpContext.Current == null); // which is false          } 

I've noticed that the HttpContext on a Controller isn't the "actual" HttpContext that you would find in System.Web.HttpContext.Current.

Is there some way to manually populate the HttpContextBase on a Controller? Or a better way to create an instance of a Controller?

like image 332
hugoware Avatar asked Oct 21 '08 20:10

hugoware


People also ask

Why HttpContext current is null?

It won't work in the scheduling related class because relevant code is not executed on a valid thread, but a background thread, which has no HTTP context associated with. Overall, don't use Application["Setting"] to store global stuffs, as they are not global as you discovered.

What is HttpContext in ASP NET MVC?

HttpContext is a type which has a static Current property that you're using to get the current context. There isn't a System. Web. Mvc.

Why session is null in MVC?

Why is Session null in the constructors of Controllers? It can be accessed from Action methods. Presumably, because the MVC Routing framework is responsible for newing-up a Controller, it just hasn't (re-)instantiated the Session at that point.

What is the use of HttpContext in asp net?

The HttpContext object constructed by the ASP.NET Core web server acts as a container for a single request. It stores the request and response information, such as the properties of request, request-related services, and any data to/from the request or errors, if there are any.


2 Answers

For now I'm going to do the following. This seems to be an acceptable fix...

public new HttpContextBase HttpContext {     get {         HttpContextWrapper context =              new HttpContextWrapper(System.Web.HttpContext.Current);         return (HttpContextBase)context;                     } } 

Where this is added to a Controller class these Controllers are inheriting from.

I'm not sure if the HttpContext being null is the desired behavior, but this will fix it in the meantime for me.

like image 151
hugoware Avatar answered Oct 01 '22 05:10

hugoware


Controllers are not designed to be created manually like you're doing. It sounds like what you really should be doing is putting whatever reusable logic you have into a helper class instead.

like image 35
Brad Wilson Avatar answered Oct 01 '22 04:10

Brad Wilson