I am using a simple method to output a image, and if the media type has a link attached to it, it will act as a link. This method has been working for months and months, but suddenly a customer complained over it not working.
My razor macro in its entirety 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 produces 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 the image itself to be the root of the cause, but the media image is absolutely as it should be and I cant see any difference. And to add to the weird-factor - the macro works just perfect with any other image. The image that fails can be found here
EDIT:
For some odd reason, if I do GetType()
on the image as Douglas suggested, and it returns a Umbraco.MacroEngines.DynamicXml
object, where it on any other image returns a string. It just keep on getting weirder and weirder.
SECOND EDIT:
I decided to throw the code out and rewrite the entire thing using the technique Kevin Hendricks suggested. Now all of a sudden, I get no errors and it works just perfectly. Only difference is a couple of .ToString()
a couple of places.
IsNullOrEmpty vs IsNullOrWhiteSpace The main difference between IsNullOrEmpty and IsNullOrWhiteSpace in C# is that IsNullOrEmpty checks if there's at least one character, while IsNullOrWhiteSpace checks every character until it finds a non-white-space character.
In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String. Empty (A constant for empty strings).
IsNullOrWhitespace(string value) returns true if the string is null, empty, or contains only whitespace characters such as a space or tab.
It sounds stupid but you might want to convert the var linkId to a string. If for some reason the generic var object sees it as an integer, Uri or different, than problems like these will arrise
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))
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With