Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, an example - adapted from an existing project. Names have been changed to protect the innocent :)</p> <p>First, the HttpHandler. This responds to a specific jQuery AJAX request that asks for the value of a session variable. As it's returning JSON, create a simple object to return the results (the return type can of course be as complex as you need): -</p> <p>(Again apologies for this being C# not VB.NET; I've tried to make it as straightforward as I can)</p> <pre><code>[Serializable] public class ResponseItem { public string SessionVariable { get; set; } public ResponseItem(string sessionVariable) { this.SessionVariable = sessionVariable; } } </code></pre> <p>For simplicity's sake, add the HttpHandler to your web project. You should download the latest version of Newtonsoft.Json and add a reference to it first. This handler simply take whatever is passed in the Request "sessionVariableName" parameter, try to retrieve a Session variable of that name and return its value as a String: -</p> <pre><code>[WebService(Namespace = "http : / / namespace not important for your test")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState { public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { string json = string.Empty; try { if (!string.IsNullOrEmpty(context.Request["sessionVariableName"])) { string sessionVariableName = context.Request["sessionVariableName"]; if (context.Session[sessionVariableName] != null) { var response = new ResponseItem(context.Session[sessionVariableName].ToString()); json = Newtonsoft.Json.JsonConvert.SerializeObject(response); } } } catch (Exception ex) { // you might want to create an "Error" type to return as JSON here } context.Response.Write(json); } } </code></pre> <p>You'll need to register your handler in web.config; I'll assume you're using IIS7 or above: -</p> <pre><code> &lt;system.webServer&gt; . . . &lt;handlers&gt; &lt;!-- Custom HTTP handlers --&gt; &lt;add name="Handler" preCondition="integratedMode" verb="*" path="Handler.ashx" type="YourProject.YourNamespace.Handler, YourProject" /&gt; </code></pre> <p>Finally, the front end. A simple test form: -</p> <pre><code>&lt;div&gt; &lt;asp:TextBox ID="txtSessionVariableName" runat="server" ClientIDMode="Static" /&gt; &lt;asp:TextBox ID="txtResponse" runat="server" ClientIDMode="Static" /&gt; &lt;input type="button" value="click me" onclick="javascript::CallHandler()" /&gt; &lt;/div&gt; </code></pre> <p>Method to call the handler: -</p> <pre><code>function CallHandler() { $.getJSON("Handler.ashx", { "sessionVariableName": $("#txtSessionVariableName").val() }, handleResult); } </code></pre> <p>Callback method to handle the result: -</p> <pre><code>function handleResult(result) { var sessionItem = eval(result); $("#txtResponse").text(sessionItem.SessionVariable); } </code></pre> <p>To test this, type the name of a session variable you know exists in your app; the handler should return it, and the callback function will write the contents to txtResponse.</p> <p>Sorry if that's a bit of a long example; let me know if you need any more help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload