Best way to return a string from an HTTPHandler to a page that POSTs this .ashx file
15,095
Solution 1
Just write the string directly to the response object in your ProcessRequest method.
public void ProcessRequest(System.Web.HttpContext context)
{
context.Response.Write(mystring);
}
Solution 2
You can generate JSON from your HTTP Handler and use jquery.post
to submit form data and get results in the ColdFusion page.
Author by
John Adams
Updated on July 19, 2022Comments
-
John Adams almost 2 years
I have an ASP.Net HTTPHandler that gets POSTed from a ColdFusion web page whose FORM looks something like:
<form name="sendToHandler" action="http://johnxp/FileServiceDemo2005/UploadHandler.ashx" method="post"> <input type="hidden" name="b64fileName" value="fileservice.asmx.xml" /> <input type="hidden" name="strDocument" value="Document" /> <input type="submit" name="submitbtn" value="Submit" />
What is the best way for this .Net Handler to return a string to the POSTing ColdFusion page?
EDIT update Aug 14, 2009:
The solution I came up in my .ashx file involves saving the URL of the .cfm file that POSTed my handler and appending a querystring with the result string(s) that I want to communicate back to ColdFusion. My CF colleague uses the presence or absence of this querystring data to format the .cfm webpage accordingly:
public void ProcessRequest(HttpContext context) { string returnURL = context.Request.ServerVariables["HTTP_REFERER"]; // posting CFM page string message = UploadFile(context); // handles all the work of uploading a file StringBuilder msgReturn = new StringBuilder(returnURL); msgReturn.Append("?n="); msgReturn.Append(HttpUtility.UrlEncode(TRIMrecNumAssigned)); msgReturn.Append("&m="); // this is just a msg with performance data about the upload operation (elapsed time, size of file, etc.) msgReturn.Append(HttpUtility.UrlEncode(message)); context.Response.Redirect(msgReturn.ToString()); }
-
John Adams over 14 yearsThanks. That works but the "string" returned needs to be used by the original posting page. By sending it back in the Response object it is visually present in the browser but how to best let the posting CF page know what the value is? If I know the URL of the HTTPReferer upon entry to my .ashx code, would it be appropriate to do something like a Response.Redirect and pass the returned string in the URL as a querystring?
-
nuiun over 14 yearsThat could certainly work, but this sounds like you might want to consider an Ajax based solution, where you submit the form, use a callback function to handle the result, and update the portion of the page that needs to display it.
-
John Adams over 14 years@Ben Doom: For method 1, I have tried Womp's original suggestion of simply returning the string in the Response object (haven't done anything with Ajax callback yet). What do you mean "handle it from the browser"? For method 2, I fear I've confused you or you've misunderstood me. The snippet I placed above is a <form> in a CF page that gets submitted to my .ASHX code (I have no "form" in my .Net side of things). My handler is like a page with only code, no HTML. My .ashx needs to be like an invisible layer between the ColdFusion UI and the .Net Web services that need to be called.
-
John Adams over 14 years@Mehrdad - I can research how to generate JSON but I'm confused about how to interpret the rest of your suggestion. Are you saying the ColdFusion page would do something other than expose a button for submitting the form? My .ashx handler code gets the form data just fine from ColdFusion - would this jquery.post be something the ColdFusion guy should do so as to somehow handle the return from my HttpHandler?
-
mmx over 14 yearsYes, you need to have control on the cold fusion page. However, the modification depends on the nature of post processing the cold fusion script needs to do. If you just require to display something client side, my method works well. You are basically adding some client side functionality that posts something somewhere and shows up the result on the page (completely on the client, no cold fusion script involved). However, if you need to do some server side processing (say, like a transaction report from an external payment service), it isn't suited.
-
Ben Doom over 14 years@John Galt: CF is server-side only, so it does not have any knowledge of what is happening on the client end unless the client sends a message. AJAX,a s you've mentioned, would be one way of doing this. A second form submit to CF (after the first to .NET) would be another. If the client doesn't interact with the form, so it just submits to the .NET service unchanged, this could be handled by CF using CFHTTP without any involvement of the client. In other words, instead of making a form to submit, CF submits the form.
-
John Adams over 14 yearsPlease see my EDIT-UPDATE for the code and explanations of how I resolved this question. Thanks to womp for ideas pointing me in the right direction.
-
BornToCode about 10 years@womp +1 for the Ajax based solution, that certainly is the way to go, then you can just get the result data from the first parameter of the
success
function like explained here