开发者

Httphandler returing an array

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

Thanks,

Subrat开发者_开发百科


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜