How to send data to httphandler

asp.net, c#, httphandler, post

Solution

Since your only restrictions are not to use a query string and cookies, why not to use form post? Consider this dummy example.

In your HTML:

<form id="form" action="DefaultHandler.ashx" method="post" style="display: none;">
    <input type="hidden" name="field1" value="abc" />
    <input type="hidden" name="field2" value="xyz" />
</form>
<a href="#" onclick="form.submit(); return false;">Handle</a>

In a handler:

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    context.Response.Write("Received:\n");
    context.Response.Write(context.Request.Form["field1"]);
    context.Response.Write("\n");
    context.Response.Write(context.Request.Form["field2"]);
    context.Response.Write("\n");
}

Adjust it for your needs. You could create the form dynamically in your JavaScript and set up fields as necessary.

Problem

I have a link on a page, which does a post back. ``` otherOptionsContainer.Controls.Add(new LiteralControl(String.Format("<a href='{0}' onclick='return {1}.exportItems();'>Export</a><br/>", exportURL, this._clientInstanceName))); ``` and http handler ``` byte[] ms_excel = some_params_from_code MemoryStream ms_excel_tream = new MemoryStream(ms_excel); context.Response.ContentType = CONTENT_TYPE_MS_EXCEL; String dateNow=DateTime.Now.ToString("dd-MMM-yyyy_HH_mm", new System.Globalization.CultureInfo("en-US")); context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=Export_{0}.xls", dateNow)); ms_excel_tream.WriteTo(context.Response.OutputStream); ms_excel_tream.Close(); ``` I need to send some_params_from_code to the httpHandler. I have some restrictions. 1. Don't use query string 2. Don't use Cookies I was trying to send data using ajax like this ``` $.ajax({ url: "_Layouts/blah/blahHandler.ashx", contentType: "application/json; charset=utf-8", data: { 'key1':'value1'}, dataType: "json", success: OnComplete, error: OnFail }); ``` but http handler writes another response object. Or another context comes to handler.

Original source