Using string.IsNullOrEmpty in a potential string in Razor in Umbraco

I use a simple method to display an image, and if the media type has a link attached to it, it will act as a link. This method works for months and months, but suddenly the client complained that it was not working.

My razor macro looks like this:

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

and it causes these two errors:

 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 

and

 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) 

I suspected that the image itself was the root of the cause, but the media image is exactly as it should be, and I see no difference. And to add to the strange factor - the macro works great with any other image. Invalid image can be found here.

EDIT:

For some odd reason, if I do GetType() on the image, as Douglas suggested, and it returns an Umbraco.MacroEngines.DynamicXml object, where it returns a string in any other image. He just keeps getting weirder and weirder.

SECOND EDITING:

I decided to throw away the code and rewrite the whole thing using the technique of Kevin Hendrix. Now all of a sudden, I get no errors and this works fine. The only difference is a pair of .ToString() several places.

+4
source share
1 answer

Sounds silly, but you can convert var linkId to string. If for some reason the common var object sees it as an integer, Uri or another, then problems like these will be sent

 var topImage = Library.MediaById(topImageId); var linkId = topImage._link; if (!string.IsNullOrEmpty(linkId.ToString())) { } 

Prefer:

 var topImage = Library.MediaById(topImageId); string linkId = topImage._link.ToString(); if (!string.IsNullOrEmpty(linkId)) { } 
+7
source

All Articles