C# 将字符串从 HTTPHandler 返回到发布此 .ashx 文件的页面的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1202070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:46:42  来源:igfitidea点击:

Best way to return a string from an HTTPHandler to a page that POSTs this .ashx file

c#asp.netpostcoldfusionhttphandler

提问by John Adams

I have an ASP.Net HTTPHandler that gets POSTed from a ColdFusion web page whose FORM looks something like:

我有一个 ASP.Net HTTPHandler,它是从 ColdFusion 网页中 POST 的,它的 FORM 类似于:

<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?

此 .Net 处理程序将字符串返回到 POSTing ColdFusion 页面的最佳方式是什么?

EDIT update Aug 14, 2009:

2009 年 8 月 14 日编辑更新:

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:

我在 .ashx 文件中提出的解决方案涉及保存 POST 处理程序的 .cfm 文件的 URL,并将查询字符串附加到我想与 ColdFusion 通信的结果字符串中。我的 CF 同事使用此查询字符串数据的存在或不存在来相应地格式化 .cfm 网页:

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());
    }

采纳答案by womp

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

只需将字符串直接写入 ProcessRequest 方法中的响应对象即可。

public void ProcessRequest(System.Web.HttpContext context)
{
    context.Response.Write(mystring);
}

回答by Ben Doom

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

如果您希望 ColdFusion 知道返回的内容,那么实际上有两种方法可以解决。

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.

第一种是将字符串返回到表单(如 womp 所建议的),然后通过提交辅助表单或其他方式从浏览器处理它。由于看起来表单中的所有值都是提供的,而不是用户提供的,这不是我会做的。

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.

第二种方法是让 CF 自己处理表单发布。假设您的表单传递的值可用于 CF,您可以使用 CFHTTP 来“伪造”表单帖子。CF 将直接返回结果,您可以从那里处理响应。

回答by Mehrdad Afshari

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

您可以从 HTTP 处理程序生成 JSON 并用于jquery.post提交表单数据并在 ColdFusion 页面中获取结果。