0

I have simple OOTB handler

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;
            }
        }
    }
}

my file structure is

enter image description here

how to call this hello world example via url? I went through many tutorials and always something was wrong... like App_Code folder etc.

What have I tried (or with with)

https://msdn.microsoft.com/en-us/library/ms228090.aspx

https://www.codeproject.com/articles/538589/theplustruthplusaboutplushttphandlersplusandpluswe

and many other things without success in my case. Any ideas?

Sebastian 506563
  • 6,980
  • 3
  • 31
  • 56
  • Any reason you're adding a handler to a web api project? – Darren Wainwright Dec 15 '16 at 16:11
  • I read that ashx are good for handling files and there was said that ashx are best for that – Sebastian 506563 Dec 15 '16 at 16:13
  • The thing is you're mixing technologies somewhat. Really, it sounds like you want to know how to accept a file in a web api - which is the better question then - like this http://stackoverflow.com/questions/34168395/how-to-post-and-receive-a-file-with-web-api - handlers do it differently (easier but not necessarily better) - i would go with controllers and not handlers in a web api project – Darren Wainwright Dec 15 '16 at 16:16

1 Answers1

2

You have to map the handler, however, it is not a controller in the mvc sense so I would it outside into a location for your application's special file type handlers.

To wire it up you need to make ASP.NET aware of the handler via the configuration.

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

Word of caution - by doing this your application is going to utilize both the MVC processing pipeline and the traditional ASP.NET pipeline.

You don't call your handler directly. Your have specified that you are interested in examining request with a certain extension and/or path, which normally would have been ignored. To test the handler, simply send a http request to an endpoint that will resolve to what you have specified the handler to handle.

Ross Bush
  • 14,648
  • 2
  • 32
  • 55