Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.Transfer to an HttpHandler

I have an IHttpHandler with the following ProcessRequest method:

public void ProcessRequest(HttpContext context) {
    int id = Convert.ToInt32(context.Request.QueryString["id"] + 151);
    var xml = XDocument.Parse("<xml><cartid>" + id + "</cartid></xml>");
    context.Response.Write(xml);
}

Which I'm trying to use from an aspx page as follows:

protected void Page_Load(object sender, EventArgs e) {
    order o = new order();
    Server.Transfer(o, false);
}

I get an HttpException: Error executing child request for handler 'PostTest.order'.

If I instead try doing the transfer like:

Server.Transfer("~/order.ashx?id=65", false)

I get an HttpException: Error executing child request for /order.ashx.

Am I doing this wrong or is there another way to accomplish what I want?

like image 666
jhunter Avatar asked Jul 28 '11 20:07

jhunter


People also ask

What is server transfer in asp net?

Server. Transfer() should be used when: we want to transfer current page request to another . aspx page on the same server. we want to preserve server resources and avoid the unnecessary roundtrips to the server.

What is an Httphandler in asp net?

An ASP.NET HTTP handler is the process that runs in response to a request that is made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes . aspx files. When users request an . aspx file, the request is processed by the page handler.

Which method is used to terminate execution of the current page and start execution of a new page using the specified URL of the page?

Redirect method redirects a request to a new URL and specifies the new URL while the Server. Transfer method for the current request, terminates execution of the current page and starts execution of a new page using the specified URL path of the page.


1 Answers

Just pass the context:

var handler = new order();
handler.ProcessRequest(Context);
Response.End();
like image 180
Mark Cidade Avatar answered Sep 20 '22 08:09

Mark Cidade