Httphandler 返回一个数组

发布于 2024-10-04 07:47:39 字数 67 浏览 2 评论 0原文

.net 中的 httphandler 可以返回值吗?如果是的话怎么办?

谢谢,

苏巴特

Can a httphandler in .net return a value? If yes how?

Thanks,

Subrat

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独孤求败 2024-10-11 07:47:39

IHttpHandler接口只实现两件事:

Boolean IsReusable {get;}

void ProcessRequest(HttpContext context);

所以不...从最严格的意义上来说,它并不意味着返回值。现在,您可以根据自己的喜好调整响应想要它(SOAP/XML/JSON)。因此,实际上,只要 HTTP 可以支持,并且使用它的客户端知道如何处理它,您就可以返回您想要的任何内容。

但是,尝试通过 HttpHandler 实现自己的服务是不明智的,因为有 更简单、更有效的方法来完成同样的事情。

The IHttpHandler interface only implements two things:

Boolean IsReusable {get;}

void ProcessRequest(HttpContext context);

So no... in the strictest sense it is not meant to return a value. Now, you can shape your response to look however you want it to (SOAP/XML/JSON). So in effect, you can return anything your heart desires so long as HTTP can support it, and the client consuming it knows how to deal with it.

However, it is ill-advised to go about trying to implement your own services via an HttpHandler as there are simpler and more efficient ways to accomplish the same thing.

眼眸 2024-10-11 07:47:39

HttpHandler 通过其 ProcessRequest(HttpContext context) 方法进行响应,您可以在该方法中修改参数 context 来告知您要发回的内容作为回应。 context.Response.ContentType 指定响应的 MIME 类型,例如响应是 text/html,因此浏览器会将其渲染到 html 页面。或者响应是video/mp4 浏览器将尝试打开它,在大多数情况下,浏览器将显示一个下载对话框。不幸的是,MIME 类型中没有 text/array (我认为将来不会有)。但是您可以将数组值作为具有特殊格式的纯文本传递,并在客户端将其反序列化。这是一个简单的例子:

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    context.Response.Write("[1,2,3,4]");
}

在 javascript 的客户端:

var response = GetTheResponse(); //maybe via ajax or something equivalent.
var myArray = eval(response);    //myArray[0]=1,myArray[1]=2...

在真实的项目中,您可能想要获取一个包含复杂对象的数组(而不仅仅是简单的数字)。因此,您需要系统化的序列化/反序列化标准,例如,将 Person 对象数组序列化为 json 字符串并将其写入响应,然后使用一些 json 实用程序在客户端将它们反序列化。

The HttpHandler responses by its ProcessRequest(HttpContext context) method, in which you can modify the parameter context to tell what do you want to send back as an response. context.Response.ContentType specifies the MIME type of the response,for example the reponse is text/html so the browser will render it to an html page.Or the reponse is video/mp4 the browser will try to open it and in most circumstances the browser will show a download dialog. Unfortunately there is no text/array in the MIME type(and i think there won't be in the future). But you can pass your array value as plain text with special formats and deserialize it at client side. Here is a simple example:

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    context.Response.Write("[1,2,3,4]");
}

and at client side in javascript:

var response = GetTheResponse(); //maybe via ajax or something equivalent.
var myArray = eval(response);    //myArray[0]=1,myArray[1]=2...

In a real project, you may want to get an array with complex objects in it(not just simple numbers). So you need systematized serialization/deserialization standards, for example, you serialize your array of Person object into json strings and write it to the reponse, and you deserialize them back at client side using some json utils.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文