C# 如何从 .cs 文件 asp.net 调用 HttpHandler

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11535754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:10:54  来源:igfitidea点击:

How to call HttpHandler from .cs file asp.net

c#asp.nethttphandler

提问by ????

I have created a http handler fro my Jquery ajax call. which is working fine the jquery call is mentioned below

我为我的 Jquery ajax 调用创建了一个 http 处理程序。工作正常 jquery 调用如下所述

 $.ajax({
    url: "Services/name.ashx",
    contentType: "text/plain",
    data: {
        CustMobile: a,
        CustName: b,
        CustEmail: c
    },
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        $("#loading").hide();
    },
    error: function () {
        $("#loading").hide();
    }
});

Now my problem is this can I call the name.ashx handler through my code behind. I am using asp.net with C#. I know that it can be called by Response.write("") writing the whole script part. Ans also Register Script of page method.
But Is there any way by which I can send request to handler and get back response from the handler using c#.

现在我的问题是我可以通过后面的代码调用 name.ashx 处理程序。我在 C# 中使用 asp.net。我知道它可以被 Response.write("") 调用,编写整个脚本部分。Ans 也注册页面方法的脚本。
但是有什么方法可以将请求发送到处理程序并使用 c# 从处理程序返回响应。

Thanks.

谢谢。

采纳答案by dknaack

You can call any http resource from code behind using HttpWebRequest(System.Net namespace)

您可以使用HttpWebRequest(System.Net 命名空间)从后面的代码中调用任何 http 资源

Sample

样本

HttpWebRequest request = (HttpWebRequest) WebRequest.Create("<YourDomain>/Services/name.ashx?CustMobile=ValueOfA&CustName=ValueOfB&CustEmail=ValueOfC");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

You need an absolute path but you can get your domain or server url from the HttpContext if you dont want to hardcode the domain

您需要一个绝对路径,但如果您不想对域进行硬编码,则可以从 HttpContext 获取域或服务器 url

More Information

更多信息

回答by ????

I found another way of doing this. If you want to access it from the same project it's is very easy.

我找到了另一种方法来做到这一点。如果你想从同一个项目访问它,这很容易。

Steps to use it in code behind

在后面的代码中使用它的步骤

  1. Basically it create a class with a class name.
  2. You can create a object of that class.
  3. Then you can call ProcessRequest.
  4. It will execute that handler.
  1. 基本上它创建一个具有类名的类。
  2. 您可以创建该类的对象。
  3. 然后就可以调用了ProcessRequest
  4. 它将执行该处理程序。

Suppose I have created a handler as follows

假设我创建了一个处理程序如下

public class HandlerName : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
       //some code
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

So you can use it as follows

所以你可以使用它如下

HandlerName obj=new HandlerName();
obj.ProcessRequest(HttpContext);

Note that you can get the current context and you need to pass it in the process request. More about HttpContext[12]

请注意,您可以获取当前上下文,并且需要在流程请求中传递它。更多关于 HttpContext [ 1 2]

Edit 1

编辑 1

You can also overload the ProcessRequestmethod in case if you need to do this.

ProcessRequest如果需要,您也可以重载该方法。

public class HandlerName : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
      // some code
    }
    public void ProcessRequest(HttpContext context, string someString)
    {
       // do your coding here
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

If you don't want to override the method the you can pass the values as follow
You can add the value in the HttpContext.Current.Items

如果您不想覆盖该方法,则可以按如下方式传递值
您可以在HttpContext.Current.Items

HttpContext.Current.Items["ModuleInfo"] = "Custom Module Info"

and get it as follow in the ProcessRequestMethod

并在ProcessRequest方法中按照以下方式获取它

public class HandlerName : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
      string contextData = (string)(HttpContext.Current.Items["ModuleInfo"]);
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}