简体   繁体   English

将参数从.aspx.cs 传递到.ashx

[英]Passing parameter from .aspx.cs to .ashx

I want to pass a string to a.ashx page.我想将一个字符串传递给 a.ashx 页面。

Normally I'll do it by setting a parameter in the.aspx page like for example: Loader="TreeLoader.ashx?passedVariable=hello"通常我会通过在 .aspx 页面中设置一个参数来做到这一点,例如: Loader="TreeLoader.ashx?passedVariable=hello"

But I want to do it programmatically on the.aspx.cs side because the value will change.但我想在.aspx.cs 端以编程方式进行,因为值会改变。

The.ashx page accepts a HTTPContext: .ashx 页面接受一个 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.正确的方法将取决于控件如何从 aspx 文件传递给 ashx。 If the handler is called from server side (using Server.Transfer method) then you can use the context object itself.如果从服务器端调用处理程序(使用Server.Transfer方法),那么您可以使用上下文 object 本身。 For example, in aspx.c file例如,在 aspx.c 文件中

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

And in ashx file在 ashx 文件中

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

Advantage being you can pass the actual object as data (and not necessarily a string).优点是您可以将实际的 object 作为数据(不一定是字符串)传递。

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"] .如果将从客户端(浏览器)端进行调用,那么您需要将数据作为查询字符串参数传递 - 例如TreeLoader.ashx?searchString=data并在 ashx 中将其用作context.Request["searchString"]

How are you calling tree handler from.aspx.cs?你如何从.aspx.cs 调用树处理程序? I think it will be same as 'TreeLoader.ashx?passedVariable=hello' for eg: Response.Redirect("TreeLoader.ashx?passedVariable=hello") .我认为它将与 'TreeLoader.ashx?passedVariable=hello' 相同,例如: Response.Redirect("TreeLoader.ashx?passedVariable=hello")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM