Tuesday, August 30, 2011

Session and .NET application

It seems session ID does not created until it is first used, i.e. you may get an error when you are trying to retrieve it the first time. To avoid this, you may consider to put something similar to below in your Global.asax file if you are using .NET


protected void Session_Start(object src, EventArgs e)
{
// http://stackoverflow.com/questions/904952/whats-causing-session-state-has-created-a-session-id-but-cannot-save-it-becaus
// This is needed in order to maintain a stable session id
// it seems the session only got created at its first used. The line below just does that!
string sessionId = Session.SessionID;
}

source: as stated in the comments of the codes.

Tuesday, August 16, 2011

MVC2 Ajax and Date

With .NET MVC2/3, you can serialize an object into JSON easily by using the JSON(object) function. However, there is a minor issue you may want to watch out. In .NET, DateTime object is serialized as an signed long integer of the milliseconds sing the January 1, 1970. If you use the normal eval() method to convert the JSON string into objects, you will get the date evaluated wrong.

Instead you should use the Sys.Serialization.JavaScriptSerializer.deserialize() function (as below) instead.

var a = Sys.Serialization.JavaScriptSerializer.deserialize(context.get_data());




Also see: http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx