PictureService 4.50.4 GetDefaultPictureUrlAsync not found

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 year ago
Hi guys,

like to retrieve the default picture url of a Product Id (Represented through "MyProductId").

var _pictureService = Nop.Core.Infrastructure.EngineContext.Current.Resolve<Nop.Services.Media.IPictureService>();

imageUrl = _pictureService.GetDefaultPictureUrlAsync(MyProductId).ToString();


The value of imageUrl has the value System.Threading.Tasks.Task`1[System.String] instead of the image name...

If i leave ".ToString()" it says: No implicite conversion to String value.

Version: 4.50.4

Appreciate any suggestions on this topic.
1 year ago
It's an 'async' method.  You need to await it:

imageUrl = await _pictureService.GetDefaultPictureUrlAsync(MyProductId);
1 year ago
Also, I think you are using the wrong method, since GetDefaultPictureUrlAsync does not have product as a param.  Look at
\Presentation\Nop.Web\Areas\Admin\Factories\ProductModelFactory.cs
var defaultProductPicture = (await _pictureService.GetPicturesByProductIdAsync(product.Id, 1)).FirstOrDefault();
(productModel.PictureThumbnailUrl, _) = await _pictureService.GetPictureUrlAsync(defaultProductPicture, 75);


Or, in file \Presentation\Nop.Web\Factories\ProductModelFactory.cs
Task<IList<PictureModel>> PrepareProductOverviewPicturesModelAsync(Product product,...
1 year ago
Hi. Could not manage to use it in the View.  
So i wrote an "old school" Data reader to load the list of Products and default pics.

Using built-in Service / Factory would be more safe and elegant, so my question on that:
How does the given code it work in a View?

Thanks and appreciate your help.
1 year ago
JoAvatar wrote:
Hi. Could not manage to use it in the View.  ....

What error / message are you getting?
1 year ago
you can use it in the _Productbox.cshmtl file you want to use.

  var _pictureService = Nop.Core.Infrastructure.EngineContext.Current.Resolve<Nop.Services.Media.IPictureService>();
        var pictures = await _pictureService.GetPicturesByProductIdAsync(Model.Id);
        var defaultPicture = pictures.FirstOrDefault();


                 string fullSizeImageUrl, imageUrl, thumbImageUrl;
                (imageUrl, defaultPicture) = await _pictureService.GetPictureUrlAsync(defaultPicture);
                (fullSizeImageUrl, defaultPicture) = await _pictureService.GetPictureUrlAsync(defaultPicture, 0);

        var defaultPictureModel = new PictureModel
            {
                ImageUrl = imageUrl,
                FullSizeImageUrl = fullSizeImageUrl
            };

you can limit the loop if you want.
  @for (var i = 0; i < pictures.Count; i++)
        {
            var picture = pictures[i];

            (imageUrl, picture) = await _pictureService.GetPictureUrlAsync(picture);
            (fullSizeImageUrl, picture) = await _pictureService.GetPictureUrlAsync(picture);
            (thumbImageUrl, picture) = await _pictureService.GetPictureUrlAsync(picture);

            var pictureModel = new PictureModel
                {
                    ImageUrl = imageUrl,
                    ThumbImageUrl = thumbImageUrl,
                    FullSizeImageUrl = fullSizeImageUrl,
                };

                            <a class="thumb-item" href="@fullSizeImageUrl" alt="" title="">
            <img src="@thumbImageUrl" data-defaultsize="@imageUrl" data-fullsize="@fullSizeImageUrl" />
            </a>
        }


in addition;

You can customize the view with these codes. standard build.

        <link rel="stylesheet" href="~/lib_npm/magnific-popup/magnific-popup.css" />
        <script asp-exclude-from-bundle="true" src="~/lib_npm/magnific-popup/jquery.magnific-popup.min.js" asp-location="Footer"></script>



<script asp-location="Footer">
            $(document).ready(function () {
                $('.picture-thumbs').magnificPopup(
                    {
                        type: 'image',
                        delegate: 'a',
                        removalDelay: 300,
                        gallery: {
                            enabled: true,
                            navigateByImgClick: true,
                            preload: [0, 1],
                            tPrev: '@T("Media.MagnificPopup.Previous")',
                            tNext: '@T("Media.MagnificPopup.Next")',
                            tCounter: '@T("Media.MagnificPopup.Counter")'
                        },
                        tClose: '@T("Media.MagnificPopup.Close")',
                        tLoading: '@T("Media.MagnificPopup.Loading")'
                    });
            });
        </script>
        <script asp-location="Footer">
            $(document).ready(function () {
                $('.thumb-item > img').on('click',
                    function () {
                        $('#[email protected]').attr('src', $(this).attr('data-defaultsize'));
                        $('#[email protected]').attr('title', $(this).attr('title'));
                        $('#[email protected]').attr('alt', $(this).attr('alt'));
                        $('#[email protected]').attr('href', $(this).attr('data-fullsize'));
                        $('#[email protected]').attr('title', $(this).attr('title'));
                    });
            });
        </script>
1 year ago
Great to have this comprehensive answer, kuzeys.

Thanks a lot!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.