Copied to clipboard

Flag this post as spam?

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


  • John 18 posts 74 karma points c-trib
    Jul 03, 2015 @ 07:36
    John
    0

    UmbracoContext.Current.PublishedContentRequest.PublishedContent Null Refernace

    I am looking to set-up a custom route and custom view model however are getting a null reference error.

    I have been using the code based on this previous post

    The Route:

    var locationRoute = RouteTable.Routes.MapRoute("location", "area/{location}", new { controller = "MyAreas", action = "MyAreas", location = UrlParameter.Optional });  
    locationRoute.RouteHandler = new PublishedPageRouteHandler((int)WellKnownPages.MyAreasPublishedPage);
    

    The Handler:

    public class PublishedPageRouteHandler : IRouteHandler
        {
            public PublishedPageRouteHandler(int pageId)
            {
                _pageId = pageId;
            }
            private readonly int _pageId;
            private UmbracoHelper _umbraco;
            public UmbracoHelper Umbraco
            {
                get { return _umbraco ?? (_umbraco = new UmbracoHelper(UmbracoContext.Current)); }
            }
            public IHttpHandler GetHttpHandler(RequestContext requestContext)
            {
                //we want to lookup the document by id
                var content = Umbraco.TypedContent(_pageId);
                if (content != null)
                {
                    requestContext.RouteData.DataTokens["umbraco"] = new RenderModel(content, CultureInfo.CurrentUICulture);
                    UmbracoContext.Current.HttpContext.Items["pageID"] = _pageId;
    
                    var docRequest = UmbracoContext.Current.PublishedContentRequest;//this is null
                    requestContext.RouteData.DataTokens.Add("umbraco-doc-request", UmbracoContext.Current.ContentCache.GetById(_pageId)); //required for RenderMvcController
                }
                return new MvcHandler(requestContext);
            }
        }
    

    The Controller:

     public class MyAreasController : RenderMvcController
        {
    
        public ActionResult MyAreas(RenderModel model, string location)
        {
            return CurrentTemplate(new LocationModel(location));
        }
    
    }
    

    The Model:

     public class LocationModel :  RenderModel
        {
            public string SelectedLocation { get; set; }
    
            public LocationsModel(string location) : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent)
            {
                  SelectedLocation = location;
            }
        }
    

    The exception happens when the model is created as UmbracoContext.Current.PublishedContentRequest.PublishedContent is null.

    Any suggestions?

  • Ben McKean 272 posts 549 karma points
    Oct 05, 2015 @ 13:06
    Ben McKean
    0

    Did you ever get a solution to this? I'm having similar issues

Please Sign in or register to post replies

Write your reply to:

Draft