+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Custom MVC Root Template Resolver and NullReferenceException

Custom MVC Root Template Resolver and NullReferenceException

3 posts, 0 answered
  1. Adrian
    Adrian avatar
    3 posts
    Registered:
    20 Apr 2015
    25 May 2015
    Link to this post

    I have Sitefinity 8.0.  

     I added a Custom Root Template resolver to my project:

     

    public class CustomMvcRootResolver : IRootTemplateResolver
    {
        public RootTemplate ResolveTemplate(RootTemplate rootTemplate, RequestContext requestContext, string theme)
        {
            //var filePath = HostingEnvironment.MapPath("~/RootTemplates/MvcFrontendTemplate.html");
            return rootTemplate.FromString(RootTemplateHtml);
        }
     
    private const string RootTemplateHtml = @"<!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta http-equiv=""content-type"" content=""text/html; charset=utf-8"" />
        <style type=""text/css"">
            body { background-color:green; color:#ffffff; }
        </style>
    </head>
        <body>
            <div class=""sfPublicWrapper"" id=""PublicWrapper"">
            </div>
        </body>
    </html>";
    }

     

    I registered it in the Application_Start of the Global asax file:

     

    PowerTools.Instance
    .RootTemplates
    .RegisterMvcTemplateResolver<CustomMvcRootResolver>();

    I then created a template, choose MVC only, created a page based on that template (I didn't add any widgets to the page), published the page, then chose View Page from the Pages screen.  

     I get the following error:

    [NullReferenceException: Object reference not set to an instance of an object.]
       Telerik.Sitefinity.Web.PageRouteHandler.handler_PreRenderComplete(Object sender, EventArgs e) +913
       System.EventHandler.Invoke(Object sender, EventArgs e) +0
       System.Web.UI.Page.OnPreRenderComplete(EventArgs e) +121
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1155

     

    Anyone know why this is happening?

  2. Adrian
    Adrian avatar
    3 posts
    Registered:
    20 Apr 2015
    26 May 2015
    Link to this post

    My code above is based on the sample code here:

    https://github.com/Sitefinity/PowerTools/wiki/Root-Templates#tool-description

     Has anyone been able to successfully implement a custom MVC Root Template resolver?  

     I really need to get this to work...

  3. Adrian
    Adrian avatar
    3 posts
    Registered:
    20 Apr 2015
    26 May 2015
    Link to this post

    The code in the sample is not entirely correct.

    The solution is:  <head> needs the runat="server" attribute.  Also, delete the <title> element.

3 posts, 0 answered