ImageUrl alternative image

Hi, 

If I have the following : 

<CardHeader Title="blabla" SubTitle="test" ImageUrl="@aURL" />

Is there a way to provide the control with an alternative url if the picture is not found ?

Regards,




7 Replies 1 reply marked as answer

RK Revanth Krishnan Syncfusion Team November 11, 2021 03:48 PM UTC

Hi Nicolas, 
 
Greetings from Syncfusion support. 
 
Currently, we are validating your query from our end, we will update you with further details on or before 15th Nov 2021. We appreciate your patience till then. 
 
Regards, 
Revanth 



IS Indrajith Srinivasan Syncfusion Team November 19, 2021 10:45 AM UTC

Hi Nicolas, 
 
Sorry for the delay in getting back to you, 
 
We have validated your reported query “Is there a way to provide the control with an alternative url if the picture is not found”. Yes, you can re-direct a new URL if the image is not loaded properly in the specified URL. We have prepared a sample meeting your requirements, by loading the alternate URL if the initial loaded image URL is not valid. Check the below code blocks for reference. 
 
Code blocks: 
 
 
@using Syncfusion.Blazor.Cards 
 
<SfCard ID="SecondCard"> 
    <CardHeader Title="Harrisburg Keith" SubTitle="Title" ImageUrl="@aURL" /> 
    <CardContent Content="Hi, I'm creative graphic design for print, new media based in Edenbridge" /> 
    <CardFooter> 
        <CardFooterContent> 
            <table> 
                <tr> 
                    <td> <div class="e-icons ChatIcon"></div></td> 
                    <td> <div class="e-icons StarIcon"></div></td> 
                    <td> <div class="e-icons AlarmIcon"></div></td> 
                </tr> 
            </table> 
        </CardFooterContent> 
    </CardFooter> 
</SfCard> 
 
@code { 
    //Invalid URL 
    public string aURL { get; set; } = "images/cards/football.png"; 
 
    protected override async Task OnInitializedAsync() 
    { 
        if (!(Uri.TryCreate(aURL, UriKind.Absolute, out Uri uriResult) && uriResult.Scheme == Uri.UriSchemeHttps)) 
        { 
        } 
    } 
} 
 
 
Please let us know if the solution helps, 
 
Regards, 
Indrajith 



NN Nicolas Navier November 19, 2021 09:43 PM UTC

Hi,

I tried your solution and I could not get it to work, that section :
Uri.TryCreate(aURL, UriKind.Absolute, out Uri uriResult)
Works in my case even if the url returns 404

like so :

What could have been possible maybe is to allow for a bit of flexibility like so:

    background-image: url(https://broken_picture.jpg),url("https://via.placeholder.com/300x300?text=Failed To Load");

When setting the ImageUrl, but I don't know if that's possible ?



IS Indrajith Srinivasan Syncfusion Team November 22, 2021 12:36 PM UTC

Hi Nicolas,

We are currently validating your reported query, and will get back to you with further details in two business days.

Regards,
Indrajith


IS Indrajith Srinivasan Syncfusion Team November 24, 2021 08:21 AM UTC

Hi Nicolas, 
 
Good day to you, 
 
When setting the image as Https links, the Uri validator just checks the link format is valid or not. In order to achieve your requirement,  we suggest you to check the link is loading properly using the HttpClient GetAsync property. Check the below code blocks for example. 
 
Code blocks: 
 
 
@using Syncfusion.Blazor.Cards 
@using System.Net.Http; 
@using System.Threading.Tasks; 
 
<SfCard ID="SecondCard"> 
    <CardHeader Title="Harrisburg Keith" SubTitle="Title" ImageUrl="@aURL" /> 
    <CardContent Content="Hi, I'm creative graphic design for print, new media based in Edenbridge" /> 
    <CardFooter> 
        <CardFooterContent> 
            <table> 
                <tr> 
                    <td> <div class="e-icons ChatIcon"></div></td> 
                    <td> <div class="e-icons StarIcon"></div></td> 
                    <td> <div class="e-icons AlarmIcon"></div></td> 
                </tr> 
            </table> 
        </CardFooterContent> 
    </CardFooter> 
</SfCard> 
 
@code { 
    //Invalid URL 
    public string aURL { get; set; } = https://broken_picture.jpg; 
    public HttpClient client = new HttpClient(); 
 
    protected override async Task OnInitializedAsync() 
    { 
        try 
        { 
            HttpResponseMessage response = await client.GetAsync(aURL); 
        } 
        catch 
        { 
        } 
    } 
} 
  
Please let us know if the solution helps, 
 
Regards, 
Indrajith 


Marked as answer

NN Nicolas Navier November 29, 2021 04:03 PM UTC

That works thanks !


--Nicolas



IS Indrajith Srinivasan Syncfusion Team November 30, 2021 05:50 AM UTC

Hi Nicolas,

Welcome,

Please get back to us if you need any further assistance.

Regards,
Indrajith



Loader.
Up arrow icon