Developer forum

Forum » Templates » Cookies in Razor C#?

Cookies in Razor C#?

Dmitrij Jazel
Reply

Hi guys,

Have a question, C# is backend, ahd HTML5 storage is client.

Was wondering if there is any easy way to access HTML5 storage in Razor, C#?

Have no possibility to pass this value to razor through Dynamicweb.Input.Request("value")

Any ideas/suggestions?

 

/Dmitrij


Replies

 
Dmitrij Jazel
Reply

I am trying to use something like this:

string cookie= Request.Cookies["test"].Value;

but I am getting Razor template error, says " 'Request' does not exist in the current context"

 
Sten Hougaard
Reply

Hi Dmitrij,

It can of cause be done, however there exists no automated functionality as there is with Cookies. Cookies are always send back and forth the client and the server, browser localstorage is not. I actually just the other day was thinking about such a setup. I have not googled it, but I guess that "someone outthere" have already created some code to handle it.

What is needed?

You need some code i .NET to handle such a setup, some C# code to support sending serialized data to the client, and to receive the data too.

The client also needs "to do something". When it posts data to the server a layer in javascript should pickup, and serialize (JSON.stringify) the data stored in the localstorage. It also needs to be able to receive JSON data in response and parse it through to the browser data layer (localstorage).

These tasks could be first started up by making a test where you define say 4 types of data you wish to exchange between server and server.

I know that this reply is not a solution, however I hope that you can find some value in it. You are welcome to contact me, should you wish to discuss it further. Also I would like to mention something from the outside of the Dynamicweb and .NET world: Meteor. Meteor is a very interesting platform which is coded in javascript, and they have invented their own protocol which can do amoung other instant client-server data binding! It sounds wild, but any data on any client can be dynamically replicated without having to code extra code. To my my note more that just empty words, I would like to share a site I have build (as a hello-world) in Meteor. It is a "leaderboard" where you get to login (Use: test@test.com/testtest) and then can give and take points, plus add your own user. Should you choose to visit the site, I will be there and you will see how any change done in any browser anywhere will be dynamically reflected across all visitors, simply by coding this in javascript in Meteor! :-) Here goes: https://netsi1964.meteor.com/

Have a great week-end.

/Sten Hougaard

Twitter: @netsi1964

#hireMe - I am looking for a new job, so if you need an experienced front-end developer, please contact me: netsi1964@gmail.com

 
Sten Hougaard
Reply
This post has been marked as an answer

Dmitry, you should look at my recipies here: https://github.com/dynamicweb/razor/wiki/Recipes

@using System.Web 

@{
var request = HttpContext.Current.Request;
var response = HttpContext.Current.Response;
}

/Sten Hougaard

Twitter: @netsi1964

#hireMe - I am looking for a new job, so if you need an experienced front-end developer, please contact me: netsi1964@gmail.com

Votes for this answer: 1
 
Dmitrij Jazel
Reply

Hi Sten :) thanks for sharing great resource :) Will check it our right away.

Regarding Parsing Json and serialization, I guess this is something that you had in mind probably? http://dotnetslackers.com/articles/aspnet/Using-HTML5-Web-Storage-in-ASP-NET.aspx

/Dmitrij

 
Dmitrij Jazel
Reply

Hi Sten,

This works perfectly :) Thanks for the tip! :)

    @using System.Web
    string test = "";    
    if (HttpContext.Current.Request.Cookies["test"] != null)
    {
        test = HttpContext.Current.Request.Cookies["test"].Value;
    }
    else
    {
        test = "test value";
        HttpContext.Current.Response.Cookies["test"].Value = test;
    }

/Dmitrij

 
Sten Hougaard
Reply

You are welocome! :-)

Good luck, the link looks great, at least as a running example for you to get started.

/Sten

 

You must be logged in to post in the forum