Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过url或其他方式从WebApi项目调用Handler.ashx?_C#_Asp.net_.net_Asp.net Web Api_Ashx - Fatal编程技术网

C# 如何通过url或其他方式从WebApi项目调用Handler.ashx?

C# 如何通过url或其他方式从WebApi项目调用Handler.ashx?,c#,asp.net,.net,asp.net-web-api,ashx,C#,Asp.net,.net,Asp.net Web Api,Ashx,我有一个简单的OOTB处理器 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace XXX.XXX.WebApi.Controllers { /// <summary> /// Summary description for Handler1 /// </summary> public class Han

我有一个简单的OOTB处理器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace XXX.XXX.WebApi.Controllers
{
    /// <summary>
    /// Summary description for Handler1
    /// </summary>
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
命名空间XXX.XXX.WebApi.Controllers
{
/// 
///Handler1的摘要说明
/// 
公共类handler 1:IHttpHandler
{
公共void ProcessRequest(HttpContext上下文)
{
context.Response.ContentType=“text/plain”;
context.Response.Write(“helloworld”);
}
公共布尔可重用
{
得到
{
返回false;
}
}
}
}
我的文件结构是

如何通过url调用这个hello world示例?我读了很多教程,但总是有点不对劲。。。如应用程序代码文件夹等

我试过(或用过)什么


在我的情况下,还有很多其他事情都没有成功。有什么想法吗?

您必须映射处理程序,但是,它不是mvc意义上的控制器,因此我会将它从外部映射到应用程序特殊文件类型处理程序的位置

要将其连接起来,您需要通过配置使ASP.NET知道处理程序

<httpHandlers>
    <add verb="*" path="*.myext" type="Handler1"/>
</httpHandlers>

注意事项-通过这样做,您的应用程序将同时利用MVC处理管道和传统的ASP.NET管道


你不能直接给你的处理者打电话。您已经指定您有兴趣检查具有特定扩展名和/或路径的请求,这通常会被忽略。要测试处理程序,只需向端点发送一个http请求,该端点将解析为您指定要处理的处理程序。

您必须映射处理程序,但是,它不是mvc意义上的控制器,因此我会将它从外部映射到应用程序特殊文件类型处理程序的位置

要将其连接起来,您需要通过配置使ASP.NET知道处理程序

<httpHandlers>
    <add verb="*" path="*.myext" type="Handler1"/>
</httpHandlers>

注意事项-通过这样做,您的应用程序将同时利用MVC处理管道和传统的ASP.NET管道


你不能直接给你的处理者打电话。您已经指定您有兴趣检查具有特定扩展名和/或路径的请求,这通常会被忽略。要测试处理程序,只需向端点发送一个http请求,该端点将解析为您指定的处理程序要处理的内容。

您将处理程序添加到web api项目中的原因是什么?我了解到ashx非常适合处理文件,有人说ashx最适合处理文件。问题是您在某种程度上混合了各种技术。实际上,听起来您想知道如何在web api中接受文件-这是一个更好的问题-就像这样-处理程序以不同的方式进行处理(更简单,但不一定更好)-我会选择web api项目中的控制器,而不是处理程序。你为什么要在web api项目中添加处理程序?我读到ashx适合处理文件,有人说ashx最适合处理文件。问题是你在某种程度上混合了技术。实际上,听起来您想知道如何在web api中接受文件-这是一个更好的问题-像这样-处理程序做得不同(更简单,但不一定更好)-我会使用控制器,而不是web api项目中的处理程序