简体   繁体   English

Umbraco Razor模板错误

[英]Umbraco Razor Template Error

I'm new to Razor templates in Umbraco (and in general), but I prefer using it over XSLT files. 我是Umbraco(以及一般)中Razor模板的新手,但我更喜欢在XSLT文件中使用它。 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
   {
     var 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. 我非常感谢任何人都可以提供的任何帮助......即使它让我知道如何在Umbraco中调试这个。 Thanks! 谢谢!

Edit: The version of Umbraco 4.6.1 编辑: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. 事实证明,Umbraco的Razor缓存过于激进或存在错误,因此在对脚本进行更改时缓存未被无效。 To work around it, I had to recycle the Application Pool in IIS. 为了解决这个问题,我不得不在IIS中回收应用程序池。 All is working now. 现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM