U4-5445 - UmbracoContext.Current is null in Custom HttpHandler after upgrading to 7.1.6

Created by Alberto Soares 05 Sep 2014, 17:39:15 Updated by Shannon Deminick 27 Apr 2017, 21:25:20

Relates to: U4-9567

UmbracoContext is null when trying to create UmbracoHelper on Custom HttpHandler after upgrading to 7.1.6.

In the version 7.1.4 the follwing code worked: UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);

The ApplicationContext is good, just lost the UmbracoContext.

Cheers

Comments

Shannon Deminick 08 Sep 2014, 06:03:49

What type of path is your custom HttpHandler binding to?

We don't create an UmbracoContext on begin request if httpContext.Request.Url.IsClientSideRequest() is true which means that :

  • the path has an extension
  • the extension for the path is not one of these: ".aspx", ".ashx", ".asmx", ".axd", ".svc"

So I'm assuming your handler has an extension and it is binding to an extension that is not one of those.

You can ensure an UmbracoContext is created very easily:

UmbracoContext.EnsureContext(
    httpContext, 
    ApplicationContext.Current, 
    new WebSecurity(httpContext, ApplicationContext.Current), 
    true);   


Alberto Soares 08 Sep 2014, 10:41:44

Hi Shannon,

Yep, we are using a "custom" binding.

The workaround worked!

Thx.


Andy Neil 10 Oct 2014, 13:17:17

Hi Shannon,

This got us too, thanks for the fix!

Andy


Lee Kelleher 20 Oct 2014, 13:35:56

Just got caught out by this one too. I'm using a custom route/controller to handle a (multi-site) dynamic XML sitemap:

e.g. routes.MapRoute("XML Sitemap", "sitemap.xml", new { controller = "XmlSitemap", action = "Index" });

This worked fine on v7.1.4, but I've since upgraded to v7.1.8 ... Which now throws this exception:

Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: umbracoContext

Here's the stacktrace:

[ArgumentNullException: Value cannot be null. Parameter name: umbracoContext] Umbraco.Web.Mvc.UmbracoController..ctor(UmbracoContext umbracoContext) +121 Umbraco.Web.Mvc.UmbracoController..ctor() +20 My.Controllers.XmlSitemapController..ctor() +29


If I'm going to use the UmbracoContext.EnsureContext workaround, where should I add it?

Thanks,

  • Lee


Shannon Deminick 20 Oct 2014, 22:44:54

@leekelleher The error is coming from a null check in the constructor. So when your controller is being constructed, the UmbracoContext singleton does not exist. The reason for this is that a *.xml path is not a managed .Net path so an UmbracoContext is not created for this request.

A simple work around is to not inherit from UmbracoController and get an UmbracoContext from EnsureContext in your ctor.

The other way is to use DependencyInjection and non-empty contructors in your site which controls how these services are resolved.


Sebastiaan Janssen 21 Oct 2014, 07:21:59

Out of curiosity, if you set runAllManagedModulesForAllRequests to true wouldn't that pick up xml as well?


Shannon Deminick 21 Oct 2014, 07:23:33

Nope, it's our code that performs this filter


Shannon Deminick 21 Oct 2014, 07:24:37

The method is IsClientSideRequest(), if that is true, we don't do anything for performance sake. But if you set that config item to false, then it shouldn't hit the module anyways for client side requests.


Sebastiaan Janssen 21 Oct 2014, 07:25:05

Good to know!


Lee Kelleher 21 Oct 2014, 08:02:10

Thanks Shannon. I ended up converting my code to a HttpHandler. (Good call on DependencyInjection)

Was one of those, "''I've just upgraded and now my code no longer works''" moments.


Sebastiaan Janssen 26 Oct 2014, 20:43:15

Just marking this a not backwards compatible so people can find out about it on the release page.


Nicholas Westby 26 Apr 2017, 16:19:45

Ran into this when upgrading from Umbraco 6.1.3 to 6.2.6. Since I didn't have a whole lot of time to dig into it, I went with a workaround of changing the route from sitemap.xml to xml-sitemap (made a corresponding change in the robots.txt). Not ideal, but works in this case.


Priority: Normal

Type: Bug

State: Closed

Assignee:

Difficulty: Normal

Category:

Backwards Compatible: False

Fix Submitted:

Affected versions:

Due in version: 7.1.6

Sprint:

Story Points:

Cycle: