4

I am facing an issue while fetching the media item URL. This is my code snippet:

@{
    Sitecore.Data.Fields.ImageField imageItem = (Sitecore.Data.Fields.ImageField)Sitecore.Context.Item.Fields["BackgroundImage"];

            var titleUrl = Sitecore.Resources.Media.HashingUtils.ProtectAssetUrl(
                               Sitecore.Resources.Media.MediaManager.GetMediaUrl(
                                   imageItem.MediaItem,
                                   new MediaUrlOptions()
                                   {
                                       Language = Sitecore.Context.Language,
                                       Height = 600
                                   }));
}

I am using this titleUrl in the image tag:

<img src="@titleUrl" />

But while fetching the URL, MediaManager is throwing this error:

System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null.
Arg_ParamName_Name
  Source=Sitecore.Kernel
  StackTrace:
   at Sitecore.Diagnostics.Assert.ArgumentNotNull(Object argument, String argumentName)
   at Sitecore.Resources.Media.MediaProvider.GetMediaUrl(MediaItem item, MediaUrlOptions options)
   at ASP._Page_Views_DeepContent_PageHeader_Index_cshtml.Execute() in c:\inetpub\wwwroot\sitecoretac\Views\Index.cshtml:line 13
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

What is the issue here? Thanks for the help.

5

From what I can tell your exception says in fact that you passed null MediaItem to GetMediaUrl method.

It looks like you have a field ImageField imageItem but imageItem.MediaItem is null. It's either not selected or the media item is not published.

Add extra check to your code to render a url only when imageItem.MediaItem is not null:

var titleUrl = imageItem.MediaItem == null
    ? null
    : Sitecore.Resources.Media.HashingUtils.ProtectAssetUrl(
    Sitecore.Resources.Media.MediaManager.GetMediaUrl(
        imageItem.MediaItem,
        new MediaUrlOptions()
        {
            Language = Sitecore.Context.Language,
            Height = 600
        }));

And in your view render <img> tag only when @titleUrl is not null.

2
  • Yes, imageItem.MediaItem is throwing null. What could be the possible reason for that because ImageField imageItem has a value?
    – PSS
    May 17 '19 at 13:09
  • 2
    Media item is not published? Go to Sitecore desktop, switch to web database and see if that item is there May 17 '19 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.