Httphandler 返回一个数组
.net 中的 httphandler 可以返回值吗?如果是的话怎么办?
谢谢,
苏巴特
Can a httphandler in .net return a value? If yes how?
Thanks,
Subrat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
.net 中的 httphandler 可以返回值吗?如果是的话怎么办?
谢谢,
苏巴特
Can a httphandler in .net return a value? If yes how?
Thanks,
Subrat
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
IHttpHandler
接口只实现两件事:所以不...从最严格的意义上来说,它并不意味着返回值。现在,您可以根据自己的喜好调整响应想要它(SOAP/XML/JSON)。因此,实际上,只要 HTTP 可以支持,并且使用它的客户端知道如何处理它,您就可以返回您想要的任何内容。
但是,尝试通过 HttpHandler 实现自己的服务是不明智的,因为有 更简单、更有效的方法来完成同样的事情。
The
IHttpHandler
interface only implements two things: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.HttpHandler
通过其ProcessRequest(HttpContext context)
方法进行响应,您可以在该方法中修改参数context
来告知您要发回的内容作为回应。context.Response.ContentType
指定响应的 MIME 类型,例如响应是text/html
,因此浏览器会将其渲染到 html 页面。或者响应是video/mp4
浏览器将尝试打开它,在大多数情况下,浏览器将显示一个下载对话框。不幸的是,MIME 类型中没有text/array
(我认为将来不会有)。但是您可以将数组值作为具有特殊格式的纯文本传递,并在客户端将其反序列化。这是一个简单的例子:在 javascript 的客户端:
在真实的项目中,您可能想要获取一个包含复杂对象的数组(而不仅仅是简单的数字)。因此,您需要系统化的序列化/反序列化标准,例如,将
Person
对象数组序列化为 json 字符串并将其写入响应,然后使用一些 json 实用程序在客户端将它们反序列化。The
HttpHandler
responses by itsProcessRequest(HttpContext context)
method, in which you can modify the parametercontext
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 istext/html
so the browser will render it to an html page.Or the reponse isvideo/mp4
the browser will try to open it and in most circumstances the browser will show a download dialog. Unfortunately there is notext/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:and at client side in javascript:
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.