Umbraco Razor 模板错误

发布于 2024-10-19 19:08:14 字数 1594 浏览 5 评论 0原文

我对 Umbraco 中的 Razor 模板(以及一般情况)不太熟悉,但我更喜欢使用它而不是 XSLT 文件。但是,我遇到了一个我不知道如何解决的问题。我收到以下消息:

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)

我的宏看起来像这样:

@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);
     }
   }
}

我真的很感谢任何人可以提供的任何帮助...即使它让我知道如何在 Umbraco 中调试它。谢谢!

编辑:Umbraco 4.6.1 的版本

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
   {
     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. Thanks!

Edit: The version of Umbraco 4.6.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2024-10-26 19:08:15

好吧,我的最终代码是这样的:

@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>
   }
}

代码没有太大变化,但显然之前运行宏时,我在某个地方出现了错误。无论我如何更改脚本,错误仍然存​​在。事实证明,Umbraco 的 Razor 缓存过于激进或存在错误,因此在对脚本进行更改时缓存并未失效。为了解决这个问题,我必须回收 IIS 中的应用程序池。现在一切正常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文