views:

552

answers:

3

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());
    }
+4  A: 

Just write the string directly to the response object in your ProcessRequest method.

public void ProcessRequest(System.Web.HttpContext context)
{
    context.Response.Write(mystring);
}
womp
Thanks. 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?
John Galt
That 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.
womp
Please 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.
John Galt
A: 

If you want ColdFusion to know what is being returned, then there are really two ways to go about it.

The first is to return the string to the form (as suggested by womp) and then handle it from the browser, either by submitting a secondary form or some other means. Since it looks like all the values in the form are being provided instead of user-supplied, this is not what I would do.

A second method would be for CF to handle the form post itself. Assuming that the values being passed by your form are available to CF, you can use CFHTTP to "fake" a form post. CF would be returned the result directly, and you could handle the response from there.

Ben Doom
@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 Galt
@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.
Ben Doom
A: 

You can generate JSON from your HTTP Handler and use jquery.post to submit form data and get results in the ColdFusion page.

Mehrdad Afshari
@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?
John Galt
Yes, 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.
Mehrdad Afshari