0

I have a asp.net mvc application and everything seems to be working fine on my Development machine however when I try to deploy and run the application on a server it gives me the following error:

System.NullReferenceException: Object reference not set to an instance of an object.

This happens on this line of code. All I am trying to do is set a value in the session.

I have this code inside a partial class of the Controller.

public partial class HomeController : BaseController
{
   public ActionResult Index(string Value)
   {
     System.Web.HttpContext.Current.Session["Test"] = "world";
     return View();
   }
}
desiguy
  • 652
  • 2
  • 9
  • 25
  • It's worth noting that you can run and scale a lot better if you operate without sessions... If you have enough context for a given request (userid via token/cookie) and the request parameters, you should be able to re-lookup anything else, and use caching rules to keep temp data. – Tracker1 Nov 22 '16 at 19:16
  • I have a need to store some variables site wide and want these variables accessible in other controllers. Can you refer to any articles which suggest your method. Thanks. – desiguy Nov 22 '16 at 19:30

1 Answers1

0

Thanks that led me to find solution here:

Solution 1 from ASP.NET MVC - Session is null worked for me.

" Solution 1:

Link: HttpContext.Current.Session is null when routing requests

Got it. Quite stupid, actually. It worked after I removed & added the SessionStateModule like so:

<configuration>
  ...
  <system.webServer>
    ...
    <modules>
      <remove name="Session" />
      <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
      ...
    </modules>
  </system.webServer>
</configuration>

Simply adding it won't work since "Session" should have already been defined in the machine.config.

Now, I wonder if that is the usual thing to do. It surely doesn't seem so since it seems so crude..."

Community
  • 1
  • 1
desiguy
  • 652
  • 2
  • 9
  • 25