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;
}
}
}
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();
}
}
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?
Does it fail if you remove all your code from the class, and only have the class definition?
PS. you dont inherit from an interface, you implement it :-)
Hi Søren,
You're right; both
UmbracoHttpHandler
andIHttpHandler
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 getObject reference not set to an instance of an object.
before I hit any breakpoints in the class:As soon as I switch it to implement IHttpHandler instead, as shown below, it starts working normally:
Any suggestions would be appreciated!
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.
is working on a reply...