SaaS CMS has officially launched! Learn more now.

MediaController not getting called

Vote:
 

Im trying to use a controller to set some parameters on my media items. So, I have PartialContentController<MediaData> which I thiought would grab any item that inherited from MediaData. The images render, but the controller is not getting called. How do I tell Optimizely to use this controller?

[TemplateDescriptor(Inherited = true)]
   public class MediaController : PartialContentController<MediaData>
   {
       private readonly UrlResolver UrlResolver;

       public MediaController(UrlResolver urlResolver) => this.UrlResolver = urlResolver;

       public override ActionResult Index(MediaData currentContent)
       {
           switch (currentContent)
           {
               case VideoFile videoFile:
                   var videoViewModel = new VideoFileViewModel
                   {
                       DisplayControls = videoFile.DisplayControls,
                       Autoplay = videoFile.Autoplay,
                       Copyright = videoFile.Copyright
                   };

                   if (PageEditing.PageIsInEditMode)
                   {
                       videoViewModel.VideoLink = this.UrlResolver.GetUrl(videoFile.ContentLink, null,
                           new VirtualPathArguments { ContextMode = ContextMode.Default });
                       videoViewModel.PreviewImage = ContentReference.IsNullOrEmpty(videoFile.PreviewImage)
                           ? string.Empty
                           : this.UrlResolver.GetUrl(videoFile.PreviewImage, null,
                               new VirtualPathArguments { ContextMode = ContextMode.Default });
                   }
                   else
                   {
                       videoViewModel.VideoLink = this.UrlResolver.GetUrl(videoFile.ContentLink);
                       videoViewModel.PreviewImage = ContentReference.IsNullOrEmpty(videoFile.PreviewImage)
                           ? string.Empty
                           : this.UrlResolver.GetUrl(videoFile.PreviewImage);
                   }

                   return PartialView(ViewRenderingConstants.Media.VideoFile, videoViewModel);
               case ImageMediaData image:
                   var imageViewModel = new ImageMediaDataViewModel
                   {
                       Name = image.Name,
                       Description = image.Description,
                       Width = image.Width,
                       Height = image.Height,
                       AlternateText = image.AltText,
                       Title = image.Title
                   };

                   if (PageEditing.PageIsInEditMode)
                   {
                       imageViewModel.ImageLink = this.UrlResolver.GetUrl(image.ContentLink, null,
                           new VirtualPathArguments { ContextMode = ContextMode.Default });
                   }
                   else
                   {
                       imageViewModel.ImageLink = this.UrlResolver.GetUrl(image.ContentLink);
                   }

                   return PartialView(ViewRenderingConstants.Media.ImageMedia, imageViewModel);
               case CustomPdfFile pdfFile:
                   var pdfViewModel = new PdfFileViewModel { Height = pdfFile.Height };

                   if (PageEditing.PageIsInEditMode)
                   {
                       pdfViewModel.PdfLink = this.UrlResolver.GetUrl(pdfFile.ContentLink, null,
                           new VirtualPathArguments { ContextMode = ContextMode.Default });
                   }
                   else
                   {
                       pdfViewModel.PdfLink = this.UrlResolver.GetUrl(pdfFile.ContentLink);
                   }

                   return PartialView(ViewRenderingConstants.Media.Pdf, pdfViewModel);
               default:
                   return PartialView(ViewRenderingConstants.Media.Index, currentContent.GetType().BaseType.Name);
           }
       }
   }
#259136
Jul 19, 2021 21:49
Vote:
 

Hi Ethan

It could be that Episerver chooses another controller to render the content, instead of your controller.

You could try splitting this into four separate controllers, one for each media type (with the last being fall back to MediaData). This will make your controllers "more specific" controllers, and more eligible to be chosen for rendering.

#259186
Jul 20, 2021 18:06
Vote:
 

Tried that. BUT, I think the issue is this: When I add an image to block as ContentReference, with a UIHint.Image, then it uses the "Image" display template. The controller is for when you add the image to content area. So, once I updated my Image display template, it did what I wanted it to do.

#259189
Jul 20, 2021 18:17
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.