我正在使用一种简单的方法来输出图像,如果媒体类型附加了链接,它将充当链接。这个方法已经用了好几个月了,但是突然有客户提示它不起作用。
我的 Razor 宏整体看起来像这样:
@using umbraco.MacroEngines
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var topImageId = Model._topImage;
if ( topImageId != null ) {
var topImage = Library.MediaById(topImageId);
var linkId = topImage._link;
string cssStyle = string.Format( "background-image:url({0});height:{1}px;", topImage.umbracoFile, topImage.umbracoHeight );
<div id="topImage"
@if(!string.IsNullOrEmpty(linkId)){
var tempNode = @Model.NodeById(linkId);
@Html.Raw(string.Format(" onclick=\"window.location.href='{0}'\"", @tempNode.Url));
cssStyle += " cursor: pointer;";
}
@Html.Raw( string.Format( "style=\"{0}\"", cssStyle ) )
></div>
}
}
它会产生以下两个错误:
Error Loading Razor Script (file: Top Image) The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments at CallSite.Target(Closure , CallSite , Type , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at ASP._Page_macroScripts_general_topImage_cshtml.Execute() in d:\inetpub\wwwroot\friendtex.com\www\macroScripts\general\topImage.cshtml:line 15
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors)
at System.Web.WebPages.WebPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at umbraco.MacroEngines.RazorMacroEngine.ExecuteRazor(MacroModel macro, INode currentPage)
at umbraco.MacroEngines.RazorMacroEngine.Execute(MacroModel macro, INode currentPage) 0.741249 0.004230
和
Error loading MacroEngine script (file: /general/topImage.cshtml, Type: ''
The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments
at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)
我怀疑图像本身是问题的根源,但媒体图像绝对是应该的,我看不出有什么不同。并且添加到奇怪的因素 - 宏与任何其他图像完美搭配。失败的图像可以是 found here
编辑:
出于某些奇怪的原因,如果我按照 Douglas 的建议对图像执行 GetType()
,它会返回一个 Umbraco.MacroEngines.DynamicXml
对象,它在任何其他图像返回一个字符串。它只会变得越来越怪异。
第二次编辑:
我决定扔掉代码并使用 Kevin Hendricks 建议的技术重写整个代码。现在突然间,我没有任何错误,而且它运行得非常完美。唯一的区别是几个 .ToString()
几个地方。
最佳答案
这听起来很愚蠢,但您可能想将 var linkId 转换为字符串。如果出于某种原因,通用 var 对象将其视为整数、Uri 或其他类型,则会出现此类问题
var topImage = Library.MediaById(topImageId);
var linkId = topImage._link;
if (!string.IsNullOrEmpty(linkId.ToString()))
{
}
偏好:
var topImage = Library.MediaById(topImageId);
string linkId = topImage._link.ToString();
if (!string.IsNullOrEmpty(linkId))
{
}
关于c# - 在 Umbraco 的 Razor 中的潜在字符串上使用 string.IsNullOrEmpty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14142032/