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();
   }
}
2
  • 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. Commented Nov 22, 2016 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. Commented Nov 22, 2016 at 19:30

1 Answer 1

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..."

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.