简体   繁体   中英

MVC Session Variable working on Localhost but not on live server?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM