Accessing UmbracoHelper in HttpHandler request

Working with UmbracoHelper and IPublishedContent in HttpHandler

If you were planning to create sitemap.xml in Umbraco, you most probably were doing it with HttpHandler. This was fine until Umbraco version 6.2.2.

If your website was running in lower version, most probably after update your HttpHandler were crashing since UmbracoContext.Current returns null.

However, there is a workaround for this. You need to initialize UmbracoContext on ProcessRequest of your HtppHandler as a first thing.

        public void ProcessRequest(HttpContext context)
        {
            UmbracoContext.EnsureContext(
                new HttpContextWrapper(HttpContext.Current),
                ApplicationContext.Current,
                true);

            //The rest of your code
        }
    

This simple fix should allow you accessing UmbracoHelper methods and published Umbraco context from App_Data/umbraco.config

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan Stojanovic is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform includion ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

.NET

read more

JavaScript

read more

SQL/T-SQL

read more

PowerShell

read more

Comments for this article