开发者

Passing parameter from .aspx.cs to .ashx

开发者 https://www.devze.com 2023-03-03 16:26 出处:网络
I want to pass a string to a .ashx page. Normally I\'ll do it by setting a parameter in the .aspx page like for example: Loader=\"TreeLoader.ashx?passedVariable=hello\"

I want to pass a string to a .ashx page.

Normally I'll do it by setting a parameter in the .aspx page like for example: Loader="TreeLoader.ashx?passedVariable=hello"

But I want to do it programmatically on 开发者_JAVA技巧the .aspx.cs side because the value will change.

The .ashx page accepts a HTTPContext:

public void ProcessRequest(HttpContext context)
    {

Shouldn't there be some way to add a parameter to the context, and then get the parameter in a way similar to this:

string searchString = context.Request["searchString"];

What is the best way to achieve this?


The correct way will depend upon how the control is passed to the ashx from the aspx file. If the handler is called from server side (using Server.Transfer method) then you can use the context object itself. For example, in aspx.c file

HttpContext.Current["key"] = data;
Server.Transfer("TreeLoader.ashx");

And in ashx file

public void ProcessRequest(HttpContext context)
{
    var data = context["key"];
    ...

Advantage being you can pass the actual object as data (and not necessarily a string).

If call will be made from client (browser) side then you need to pass data as query string parameter - such as TreeLoader.ashx?searchString=data and use it in ashx as context.Request["searchString"].


How are you calling tree handler from .aspx.cs? I think it will be same as 'TreeLoader.ashx?passedVariable=hello' for eg: Response.Redirect("TreeLoader.ashx?passedVariable=hello").

0

精彩评论

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