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.
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:
The Handler:
The Controller:
The Model:
The exception happens when the model is created as UmbracoContext.Current.PublishedContentRequest.PublishedContent is null.
Any suggestions?
Did you ever get a solution to this? I'm having similar issues
is working on a reply...