Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Zweben 266 posts 750 karma points
    Aug 12, 2019 @ 21:09
    David Zweben
    0

    Implementing UmbracoHttpHandler?

    I am trying to implement UmbracoHttpHandler with the code below. The handler is linked up in the media folder's web.config. Everything works fine when the class inherits from IHttpHandler, but when I switch it to inherit from UmbracoHttpHandler as shown below, I start getting Object reference not set to an instance of an object. immediately upon trying to load any media, before it hits any breakpoints in MediaHttpHandler.cs.

    Any ideas what I am doing wrong?

    public class MediaHttpHandler : UmbracoHttpHandler
    {
        public override bool IsReusable => true;
    
        public override void ProcessRequest(HttpContext context)
        {
            string requestedFile = context.Server.MapPath(context.Request.FilePath);
    
            switch (Path.GetExtension(requestedFile))
            {
                case ".jpg":
                    context.Response.ContentType = "image/jpg";
                    break;
    
                // Etc... several more of these.
    
                default:
                    context.Response.ContentType = "text/plain";
                    break;
            }
    
            if (requestedFile != null)
            {
                context.Response.TransmitFile(requestedFile);
                context.Response.End();
            }
            else
            {
                context.Response.StatusCode = 404;
            }
        }
    }
    
  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Aug 13, 2019 @ 04:44
    Søren Gregersen
    0

    Does it fail if you remove all your code from the class, and only have the class definition?

    public class MediaHttpHandler : UmbracoHttpHandler {}
    

    PS. you dont inherit from an interface, you implement it :-)

  • David Zweben 266 posts 750 karma points
    Aug 13, 2019 @ 13:12
    David Zweben
    0

    Hi Søren,

    You're right; both UmbracoHttpHandler and IHttpHandler are 'implemented', actually, because UmbracoHttpHandler is an abstract class.

    I can't have a completely empty implementation because UmbracoHttpHandler is abstract, but I tried the simplest implementation possible (shown below), and I still get Object reference not set to an instance of an object. before I hit any breakpoints in the class:

    using System.Web;
    using Umbraco.Web;
    
    public class MediaHttpHandler : UmbracoHttpHandler
    {
        public override bool IsReusable => true;
    
        public override void ProcessRequest(HttpContext context)
        {
            throw new System.NotImplementedException();
        }
    }
    

    As soon as I switch it to implement IHttpHandler instead, as shown below, it starts working normally:

    using System.Web;
    using Umbraco.Web;
    
    public class MediaHttpHandler : IHttpHandler
    {
        public bool IsReusable => true;
    
        public void ProcessRequest(HttpContext context)
        {
            throw new System.NotImplementedException();
        }
    }
    

    Any suggestions would be appreciated!

  • David Zweben 266 posts 750 karma points
    Aug 13, 2019 @ 13:17
    David Zweben
    100

    Since this seemed to be a bug, I searched the issue tracker and found this issue, which seems likely to be what I'm running into:

    https://github.com/umbraco/Umbraco-CMS/issues/6018

    Therefore, I'll move my discussion over to there.

Please Sign in or register to post replies

Write your reply to:

Draft