Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Charlie 14 posts 95 karma points
    Feb 19, 2018 @ 19:28
    Charlie
    0

    Is Umbraco overriding my session, do I need to do something special to work with session values in Umbraco?

    Hi I am having a weird problem, and am hoping someone can shed some light on it.

    I am creating my first Umbraco application, I get the users location and determine the closest store location for them. I need to set this value in the session.

    I am getting the location and storing it in the session, but then when I go to access it, the session variable is gone.

    In My layout:

    <script type="text/javascript">
        var locID = 1;
    
        $(document).ready(function () {
            $.ajax({
                url: '@Url.Action("GetLocation", "Home", null)',
                data: { lon: longitude, lat: latitude },
                async: false,
                success: function (result) {
                    console.log(result);
                    var parsedResult = JSON.parse(result);
                    locID = parsedResult.LocationID;
    
                    $.ajax({
                        url: '@Url.Action("SetLocationInSession", "Home", null)',
                        data: { locationID: locID },
                        async: false,
                        success: function () {
                            console.log("Location set in session");
                        },
                        error: function (data) {
                            console.log(data);
                        }
                    });
                }
            });
    
            $(".search").click(function () {
                $.ajax({
                    url: '@Url.Action("GetLocationInSession", "Home", null)',
                    data: { key: "location" },
                    async: false,
                    success: function (result) {
                        var parsedResult = JSON.parse(result);
                        locID = parsedResult.LocationID;
                        $("#locationID").val(locID);
                    },
                    error: function (data) {
                        console.log(data);
                    }
                });
            });
        });
    

    The set session seems to work fine. In my controller (a surfaceController being rendered as a partial view on an Umbraco page if that matters) looks like this:

    public ActionResult GetLocation(double lon, double lat)
        {
            GeoHelper geoHelper = new GeoHelper();
    
            Context _db = new Context();
    
            var locations = _db.Locations.ToList();
    
            var location = geoHelper.GetClosestLocation(lat, lon, locations);
    
            LocationViewModel closest = new LocationViewModel();
            closest.LocationID = location.LocationID;
            closest.Name = location.Name;
            closest.Longitude = location.Longitude;
            closest.Latitude = location.Latitude;
    
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            string result = javaScriptSerializer.Serialize(closest);
    
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    
        public ActionResult SetLocationInSession(int locationID)
        {
            //Session.SetDataInSession<int>("location", locationID);
    
            Session["location"] = locationID;
    
            return Json(locationID, JsonRequestBehavior.AllowGet);
        }
    
        public ActionResult GetLocationInSession(string key)
        {
            //int locationID = Session.GetDataFromSession<int>(key);
    
            int locationID = (int)Session["location"];
    
            Context _db = new Context();
    
            var location = _db.Locations.Find(locationID);
    
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            string result = javaScriptSerializer.Serialize(location);
    
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    

    If I run the app locally with debugging I can see that in the SetLocationInSession seems to be executing fine. I hover over session, and it a key count of 1, and the the key is location, and the value is what I expect.

    But then when it calls GetLocationInSession I get an object reference error. If I put a break point here, the Session object does not have any keys. Does Umbraco need to interact with the session differently than standard ASP.NET MVC, I have done similar things to this in non Umbraco applications and never had issues.

    Thanks, Charlie

  • Charlie 14 posts 95 karma points
    Feb 19, 2018 @ 23:43
    Charlie
    1

    I'm still not sure why this fixed it, but in case anyone runs into a similar issue, for some reason changing:

    int locationID = (int)Session["location"];
    

    to:

    int locationID = (int)System.Web.HttpContext.Current.Session["location"];
    

    fixed it for me. I guess it must have been trying to reference some other session. If anyone has any insight on that, I would love to understand what session I was referencing before.

Please Sign in or register to post replies

Write your reply to:

Draft