Master Page Image Path Not Found Problem in Asp.Net C# VB.Net

As you knew that master page uses absolute image URL which cause problem to reference an image inside other folder. The problem occurs due to below reasons:

1. Master page is in the root folder
2. Images under another root folder
3. Aspx pages under another root folder

As a result you will get “Image not found” or “Image file path not exist” error.

Scenarios like below will help you to understand the problem:
Master Page Image Path Problem 1

To set the master page image path to reference an image based on above scenario you may write a code like:

<img alt="" src="Images/logo1.png"/>

Which is actually creating the problem because path will be different for different pages. The solution is simple and you may follow the below code to resolve the error:

<img alt="" src="<%= Page.ResolveUrl("~")%>Images/logo1.png"/>

If you not yet understand the problem then continue to read to run an example. Add a folder named Images also add a folder named CRMM. Now put an image under images folder plus add an aspx page(must include previously defined master page) under CRM folder.

Now modify the Masterpage code like below:

    <form runat="server">
    <div class="page">
        <div class="header">
            <div class="title">
            <img alt="" width="100px" height="100px" src="<%= Page.ResolveUrl("~")%>Images/logo1.png"/>
                    My ASP.NET Application
            <div class="loginDisplay">

Now write a line in frmCustomer Page under CRM folder:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
This is Content Page.....

Now run the project. Hope image path problem will be resolved:
Master Page Image Path Problem 2

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *