Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robin Hansen 135 posts 368 karma points
    Jun 16, 2021 @ 16:14
    Robin Hansen
    0

    Need to return empty JsonResult

    I'm in the need for making some null checks. The code below fetches images from a mediapicker (categoryItemImages) and it works as intended... as long as the dataType HAS any images in it. If the data type is empty (wich can occur) my code breaks at 'content.GetValue' - i cannot even do a nullcheck at typedMultiMediaPicker. Can someone tell me how to return an empty JsonResult if my datatype is empty...? - tnx iin advance... :-)

    public class CategoryItemsApiController : UmbracoApiController
    {
        //https://our.umbraco.com/forum/using-umbraco-and-getting-started/100600-nestcontent-get-data-is-always-in-surface-controller
        //GET: /umbraco/api/categoryitemsapi/getcategoryitemimages?nodeId=1169
        [HttpGet]
        public JsonResult<IEnumerable<CategoryImages>> GetCategoryItemImages(int nodeId)
        {
            IContentService contentService = Services.ContentService;
            var content = contentService.GetById(nodeId);
    
            var typedMultiMediaPicker = content.GetValue("categoryItemImages").ToString();
            List<string> tempMedia = typedMultiMediaPicker.Split(',').ToList();
    
            var listCategoryItemImages = new List<CategoryImages>();
    
            foreach (var item in tempMedia)
            {
                var mediaItems = Umbraco.Media(item);
                int id = mediaItems.Id;
                var categoryImage = new CategoryImages()
                {
                    id = id,
                    udi = Udi.Create("media", Guid.Parse(mediaItems.Key.ToString())),
                    src = mediaItems.Url()
                };
    
                listCategoryItemImages.Add(categoryImage);
    
            }
            return Json<IEnumerable<CategoryImages>>(listCategoryItemImages.ToList());
        }
    }
    
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jun 16, 2021 @ 18:18
    Dan Diplo
    1

    You can check if a property has a value using content.HasValue("categoryImages") and then return Json<Enumerable.Empty<CategoryImages>>() as the result if it is. Maybe that would work?

  • Robin Hansen 135 posts 368 karma points
    Jun 17, 2021 @ 06:26
    Robin Hansen
    0

    The IContentService does'nt have a 'HasValue' property - I've allready tried that... :-)

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Jun 17, 2021 @ 06:37
    Dennis Adolfi
    101

    Hi Robin. Just a heads up: The ContentService will hit the database directly which could give you performance issues.

    In your code snippet you are first accessing the node via the contentService (database call) using the nodeId, then you are resolving the image id:s from a property and then you are calling the UmbracoHelper (Umbraco.Media) to resolve those images.

    In other words: You are resolving the content node from the database directly, but you are resolving the media items from the cache. Is there a reason for this setup or is it a mistake?

    Instead of resolving the "content" item from the ContentService, could you use Umbraco.Content(nodeId) instead? That way all your communication will be with the cache, PLUS you will get an IPublishedContent instead of a IContent, and IPublishedContent will give you a HasValue() method, which will solve your original problem.

    Cheers.

  • Robin Hansen 135 posts 368 karma points
    Jun 17, 2021 @ 08:07
    Robin Hansen
    1

    @Dennis Adolfi - You're no less than a genius! - off course I should'nt use the IContent as in the linked tutorial... - some times You can stare yourself bild on the most simple things... now my code looks like below, and works as intended...! :)

    Hi5...!

    public class CategoryItemsApiController : UmbracoApiController
    {
        //GET: /umbraco/api/categoryitemsapi/getcategoryitemimages?nodeId=1169
        [HttpGet]
        public JsonResult<IEnumerable<CategoryImages>> GetCategoryItemImages(int nodeId)
        {
            var node = Umbraco.Content(nodeId);
    
            if (node.HasValue("categoryItemImages"))
            {
                var listCategoryItemImages = new List<CategoryImages>();
                var typedMultiMediaPicker = node.Value<IEnumerable<IPublishedContent>>("categoryItemImages");
                foreach (var item in typedMultiMediaPicker)
                {
                    var categoryImage = new CategoryImages()
                    {
                        id = item.Id,
                        udi = Udi.Create("media", Guid.Parse(item.Key.ToString())),
                        src = item.Url()
                    };
    
                    listCategoryItemImages.Add(categoryImage);
                }
    
                return Json<IEnumerable<CategoryImages>>(listCategoryItemImages.ToList());
            }
            else { 
                return Json(Enumerable.Empty<CategoryImages>());
            }
        }
    }
    
  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Jun 17, 2021 @ 08:36
    Dennis Adolfi
    0

    Awesome, glad I could help!!!

Please Sign in or register to post replies

Write your reply to:

Draft