3

In my MVC5 application, I have a data-grid which has it's filter/sort criteria specified inside the URL. When a user selects a Record to Edit/Create/Delete, I needed to save the URL value so that when the Controller POST action completed, they would be returned to the exact same layout they were on before the GET action was triggered (same data-grid with sort/filter criteria applied).

In my pursuit of this funcitonality, I began using a session variable in my Controller. On GET:

Session["returnURL"] = Request.UrlReferrer.AbsoluteUri;

I set a session variable of returnURL to the current full path URL of my Browser. The on the post, after my changes to the record/database are saved, I have check the returnURL variable for null and perform a Redirect():

var returnURL = (Session["returnURL"] != null) ? Session["returnURL"].ToString() : Url.Action("Index", "Home");
return Redirect(returnURL);

This all works perfectly fine on my localhost, but when I publish it to my Server each time I attempt to navigate to Edit/Create/Delete actions for my record, the Session Variable on the GET actions causes:

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. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: [No relevant source lines]

After a lot of trial and error I diagnosed that it is my Session Variable that is causing this NullReferenceException as when I remove that specific code line from my GET action method, everything else functions fine and the appropriate View loads.

Can anyone helps with this? I'm at a loss for why my Session Variable is working on my Localhost but not on my Server. I took a look at the following MSDN article https://msdn.microsoft.com/en-us/library/h6bb9cz9(v=vs.71).aspx, but I'm still getting the same result even with <configuration><system.web> <sessionState mode="InProc"></sessionState>... specified in my Web.Config.


EDIT:

At ps2goat's suggestion, I modified my Web.Config to include:

<system.web>
      <sessionState mode="InProc"></sessionState>
      // ....
      <pages enableSessionState="true">
        <namespaces>
          <add namespace="GridMvc" />
          <add namespace="MvcSiteMapProvider.Web.Html" />
          <add namespace="MvcSiteMapProvider.Web.Html.Models" />
        </namespaces>
      </pages>
      // ....
 </system.web>

Still receiving the same NullReferenceException error...? I have now specified the sessionState = InProc and set <pages enableSessionState="true">.

Analytic Lunatic
  • 3,853
  • 22
  • 78
  • 120
  • Do you have SessionState enabled on the page? Either in the web.config's `` element or in the page itself? – ps2goat Jul 27 '15 at 16:31
  • @ps2goat can you elaborate a little further? I'm afraid this is my first attempt at using Session Variables. All I did was make the appropriate .dll reference and then setup my Session Variable in code as designated above. It worked in localhost so I figured it was enabled? – Analytic Lunatic Jul 27 '15 at 16:43
  • If you have access to IIS Manager you can check the Session State feature and [Configuring Session State in IIS](https://technet.microsoft.com/en-us/library/cc754450(v=ws.10).aspx) might be helpful. – Jasen Jul 27 '15 at 17:49
  • @Jasen I'll take a look, but I know for a fact that other apps on the same server are successfully using Session variables. – Analytic Lunatic Jul 27 '15 at 18:07
  • look at the accepted answer here: http://stackoverflow.com/questions/14334147/session-state-can-only-be-used-when-enablesessionstate-is-set-to-true-either-in – ps2goat Jul 27 '15 at 18:08
  • @ps2goat, please see my EDIT above. I am getting the same result as before. – Analytic Lunatic Jul 27 '15 at 19:40
  • Do you have any attributes on your controller? My previous hint was geared more toward `webforms`, not `mvc`. – ps2goat Jul 27 '15 at 20:51
  • No major attributes as I understand it: `public async Task Edit(int id) { Session["returnURL"] = .... }` and `[HttpPost][ValidateAntiForgeryToken] public async Task Edit([Bind(Include = "Id,Field1,Filed2,etc.")] MODEL_ENTITY mod_Entity) { ... }` Nothing that is sticking out to me? – Analytic Lunatic Jul 27 '15 at 21:07

1 Answers1

2

I found the answer in this post (The Session object is null in ASP.NET MVC 4 webapplication once deployed to IIS 7 (W 2008 R2)), wherein I modified my <modules> section to do an add/remove of the below specified values:

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

I'm still not entirely sure WHY this is the answer, but it worked for my issue in this case.

Analytic Lunatic
  • 3,853
  • 22
  • 78
  • 120