I'm new to Razor templates in Umbraco (and in general), but I prefer using it over XSLT files. However, I have run into a problem that I don't know how to solve. I am getting the following message:
An unknown error occured while rendering the following code:
System.NullReferenceException: Object reference not set to an instance of an object.
at RazorEngine.Dynamic.baeffbebc.Execute()
at RazorEngine.Templating.TemplateService.Parse[T](String template, T model, String name)
at umbraco.MacroEngines.RazorEngine.GetResult(String cacheIdentifier, String template, INode currentPage, String& result)
My macro looks like this:
@using System
@using uComponents.Core
@using uComponents.Core.uQueryExtensions
@using System.Linq
@using umbraco.NodeFactory
@helper NoPictures()
{
<li>Pictures coming soon!</li>
}
@helper Pictures(String crop)
{
<li><a rel="photos" href="@crop" title="test">
<img src="@crop" class="shadow hovershadow"></a></li>
}
@{
var n = Node.GetCurrent();
var pictures = n.GetProperty("pictures").Value;
if(pictures.Length <= 0)
{
NoPictures();
}
else
{
va开发者_StackOverflowr pictureNodes = pictures.Split(',');
foreach (var pictureNode in pictureNodes)
{
var node = new Node(Convert.ToInt32(pictureNode));
var photoId = node.GetProperty("picture").Value;
var photo = uComponents.Core.uQuery.GetMedia(Convert.ToInt32(photoId));
var crop = MediaExtensions.GetImageCropperUrl(photo, "umbracoFile", "wide");
Pictures(crop);
}
}
}
I really appreciate any help that anyone can offer... even if it is giving me an idea how to debug this within Umbraco. Thanks!
Edit: The version of Umbraco 4.6.1
Okay, my final code was this:
@using System
@using uComponents.Core
@using uComponents.Core.uQueryExtensions
@using System.Linq
@{
var n = uQuery.GetCurrentNode();
var pictures = n.GetProperty("pictures").Value;
if(pictures.Length > 0)
{
var pictureNodes = pictures.Split(',');
foreach (var pictureNode in pictureNodes)
{
var node = uQuery.GetNode(Convert.ToInt32(pictureNode));
var photoId = node.GetProperty("picture").Value;
var photo = uQuery.GetMedia(Convert.ToInt32(photoId));
var crop = photo.GetImageCropperUrl("imageCropper", "wide");
<li><a rel="photos" href="@crop" title="@node.GetProperty("title").Value">
<img src="@crop" height="150px" width="150px" class="shadow hovershadow"></a></li>
}
}
else
{
<li>Pictures coming soon!</li>
}
}
The code didn't change much, but apparently when running the macro before, I had an error somewhere. No matter what I did to change the script, the error persisted. It turns out that the Umbraco's Razor caching is too aggressive or has a bug, so the cache was not being invalidated when a change was made to the script. To work around it, I had to recycle the Application Pool in IIS. All is working now.
精彩评论