The importance of giving Umbraco templates correct aliases

With the Umbraco 6.0.0 beta being released I thought it was about time that I had a play with the ASP.Net MVC features now available in Umbraco. I wanted to remake a site I had built using MVC exclusively as this would allow me to make a direct comparison between the 2 frameworks.

Unfortunately I found that uSiteBuilder played up big time with 6.0.0, occasionally working, occasionally failing with the glorious Yellow Screen of Death and occasionally procrastinating away on towards infinity. Unfortunately the latter was the most common, making it almost impossible to write up a bug report, let alone fix it. With the very latest version of Umbraco 4 (4.11.3.1) it worked perfectly though, so I figured I’d have an experiment with MVC anyway even if I can’t test the new Umbraco API.

After the obligatory setup of Umbraco and uSiteBuilder changing from Web Forms mode into MVC was a doddle, just one change in umbraco.config. Before running uSiteBuilder I created all the blank templates referred to in the code first classes as .cshtml files rather than the .master files used previously. As Umbraco stores all these files in one folder I have my own naming convention to make the solution a little more organised.

Master
Page
Page.Blog.Article
Page.Blog.Hub
…
Page.Home
…
etc

I ran uSitebuilder and all was fine, so I got cracking with testing the very basics of MVC razor vs the classic master pages.

These were my Views:

Master.cshtml
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
 Layout = null;
}
@RenderSection("MainSection", required: true)
Page.cshtml
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
 Layout = "Master.cshtml";
}
@section MainSection
{
 <!doctype html>
 <html lang="en">
    <head id="Head1">
       <title>Hi</title>
    </head>
    <body>
       @RenderSection("MainSection", required: true) 
       @RenderBody()
    </body>
 </html>
}
Page.Home.cshtml
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
 Layout = "Page.cshtml";
}




<p>Hi defaut body.</p>




@section MainSection
{
   <h1>Hello, World!</h1>
}

Problems

So I went to http://server/home to test it out, but for something seemingly so simple I seemed to get errors that were confusing.

The file "" cannot be requested directly because it calls the "RenderBody" method
The file “” cannot be requested directly because it calls the “RenderSection” method

The Page.Home View has no RenderBody() or RenderSections() in it. I trawled through MVC tutorials and stack overflow questions trying to find an answer but to no avail, the code was right but for some reason Umbraco was getting the Page View not the Page.Home View.

Solution

Finally I decided to check whether having a dot (full stop, period, “.”) in the alias of the template was breaking the way that views were fetched. Sure enough I changed the alias to PageHome and the simple site started to work.

I’m not sure whether this is an Umbraco or MVC bug / design decision, but it sure was a frustrating one to find.

So I’m off to try and decide how to change my naming convention, I’m pretty sure that that will include abandoning all attempts to mimic hierarchy in the name.

Comments

comments powered by Disqus