Library class not available in global helper class
I'm trying to write a razor helper accessible to all of my razor templates. The cshtml file for the helper has been placed in the Umbraco website App_Code directory. I'm trying to use Library.MediaById to pull back the media object for a specific media id. However I'm getting an exception: The name 'Library' does not exist in the current context.
Hi. 'Library' is actually an instance property, whereas a helper is internally compiled to a static method. You cannot call any instance member from a static member.
Thank you, Rodion, this does certainly work well! (And saved my bacon!)
However, it seems kind of like .... odd usage ... and worries me this may break unexpectedly at some point in the future? Even looking into the source code, as I find myself doing more and more of these days, it doesn't seem too clear that passing null to the RazorLibraryCore constructor is OK or not.
If so, and if this really is the recommended approach, it seems maybe cleaner if the Umbraco team would overload this constructor to not require any constructor at all, or create a static node loader we could use instead... just to show that this is OK and supported thing to do??
It occurs to me now that I suppose I could always pass Library into my helpers as one of the parameters? This would mean my (static, global) helper method would have access to a "properly-instantiated" RazorLibraryCore object instead of the "null-instantiated" one... I would just need to adjust the signature and all usage to do this I guess.
Library class not available in global helper class
I'm trying to write a razor helper accessible to all of my razor templates. The cshtml file for the helper has been placed in the Umbraco website App_Code directory. I'm trying to use Library.MediaById to pull back the media object for a specific media id. However I'm getting an exception: The name 'Library' does not exist in the current context.
The razor template below:
Any ideas?
Hi. 'Library' is actually an instance property, whereas a helper is internally compiled to a static method. You cannot call any instance member from a static member.
Right. is there a static method that can be used to load a media item given an id?
It seems that
should work well.
Thank you, Rodion, this does certainly work well! (And saved my bacon!)
However, it seems kind of like .... odd usage ... and worries me this may break unexpectedly at some point in the future? Even looking into the source code, as I find myself doing more and more of these days, it doesn't seem too clear that passing null to the RazorLibraryCore constructor is OK or not.
If so, and if this really is the recommended approach, it seems maybe cleaner if the Umbraco team would overload this constructor to not require any constructor at all, or create a static node loader we could use instead... just to show that this is OK and supported thing to do??
Thank you!
It occurs to me now that I suppose I could always pass Library into my helpers as one of the parameters? This would mean my (static, global) helper method would have access to a "properly-instantiated" RazorLibraryCore object instead of the "null-instantiated" one... I would just need to adjust the signature and all usage to do this I guess.
So fitting in with the OP example, instead of:
We would see this:
And then BAM! Pass it in each time and we have access to good ol' @Library in its intended state. Yet another trick to throw into the bag!
Best Regards!
is working on a reply...