Question

I'm quite new to Nancy so hopefully I'm just doing something silly here. I've got a nancy service which I'm posting data to like so:

$.ajax({
    type: 'POST',
    url: url,
    data: JSON.stringify({
        searchTerm: productSearchTerm,
        pageSize: pageView.PageSize(),
        selectedBrands: pageView.checkedBrands(),
        pageNumber: pageView.CurrentPage(),
        selectedCategories: pageView.checkedCategories(),
        selectedGender: pageView.checkedGender(),
        SelectedColours: pageView.checkedColour(),
        saleItemsOnly: pageView.saleItemsOnly(),
        selectedMinimumPrice: pageView.minPrice(),
        selectedMaximumPrice: pageView.maxPrice()
    }),
    contentType: "application/json; charset=utf-8",
    dataType: 'json'
})
    .done(function (data) {
        bindSearchResult(data);
    })
    .fail(function (a) {
        console.log(a);

    });

Then in the service I need to hold on to a bunch of string values for future requests from the user, which I'm doing like this:

    private void AddListOfStringToIsSessionNull(string name, IEnumerable<string> data)
    {
        if (Session[name] == null)
        {
            Session[name] = data.ToList();
        }
    }

These seems to set the session variables and an "_nc" cookie is present when I inspect the page after it returns.

However if I then F5 the page the session items are all null again at the server.

I've ruled out are cross site posting as it's all on the same domain.

Could this be an AJAX thing? Seems unlikely as this seems a pretty standard thing to do. Or can you not set it on a POST? If so is there a way around this?

If someone could help I'd be forever grateful as otherwise I'm going to have to revert back to writing this in WCF which will make me hurl myself from the window :)

Thanks a lot.

Edit

  • Open a new incognito window in Chome I hit home page, no nancy cookie present (which is correct)
  • Enter a search term which calls back over and AJAX post and grabs JSON, also pops a list of strings in the Nancy Session
  • Check cookie, a nancy one has appeared like so and the session value is correct on post back:

      npTBnqPp99nLd5fU0%2btJbq%2fY%2bdf2UFWTaq5D28Az7Jw%3dzF8cIHNxTWX399sbmowjheho2S29ocpKs1TXD51BrbyPPNCeLfAcYqWhkRHqWdwKJNED5kuspllIjhI5rf2W6NKtf8xo68BlF5eLLgJxMtAxw2yD2ednEzUazq1XBt2Id77t5LE5tZVwkpRGDT5b9J0nQnr9zfzCOALXb2hQQGBPkMVyNNTO24pW1UC6Uda3B86LLYA02Jgy4G9DiT6KsutR3pSXO8AZFOlcmAEHbSSX9A8FAHaL ... etc.
    
    • I then search for a different search term which calls this bit of code: --Session.DeleteAll();
    • The nancy session is re-populated with new data and returns back to the browser

However at this point the cookie has not been updated with the new value it is still as below:

    npTBnqPp99nLd5fU0%2btJbq%2fY%2bdf2UFWTaq5D28Az7Jw%3dzF8cIHNxTWX399sbmowjheho2S29ocpKs1TXD51BrbyPPNCeLfAcYqWhkRHqWdwKJNED5kuspllIjhI5rf2W6NKtf8xo68BlF5eLLgJxMtAxw2yD2ednEzUazq1XBt2Id77t5LE5tZVwkpRGDT5b9J0nQnr9zfzCOALXb2hQQGBPkMVyNNTO24pW1UC6Uda3B86LLYA02Jgy4G9DiT6KsutR3pSXO8AZFOlcmAEHbSSX9A8FAHaL.... etc.

Is there anything else I need to do to solve this?

Was it helpful?

Solution

So my issue was me being a bit daft really, the implementation of the cookie stuff works well, however there were occasions when I was stuffing too much into the cookie and pushing it over the 4K cookie limit.

This meant that I was seeing some inconsistent behavior where sometimes the cookie worked nicely (the cookie was < 4K) where as for some search terms too much was being written into the cookie which meant either the cookie was never created or it was not overwriting the existing cookie.

So yes, my fault, but thought this answer might aid someone as silly as me trying to store the world in a cookie..

Right I'm off to write a session provider.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top