I would suggest that you investigate ASP.NET AJAX Page Methods. They are essentially web services hosted inside of an ASP.NET page, like this:
[WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
Now you can invoke the page method via jQuery, like this:
$.ajax({
type: "POST",
url: "YourPage.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});
Note: ASP.NET AJAX Page Methods must be static and do not have an instance of the Page
class, but they do not have access to the HttpContext.Current.Session
object if you decorate the page method correctly.
Finally, ASP.NET AJAX Page Methods JSON-encode their responses, so you will not see any serialization code in the page method, because it is done automatically.