Saturday, February 10, 2018

Images are not loading inside repeater when deploying web application to IIS

Leave a Comment

I've deployed my asp.net web application following Microsoft Docs (Deploy to Test).

After deploying my application to IIS there is problem that images inside Repeater control are not loading. While in debugging/development these are loading correctly.

Below is my code (Repeater with Image control):

<asp:Repeater ID="rpBanquetList" runat="server">     <ItemTemplate>         //... other controls         <asp:Image ID="imgImagePath" ImageUrl='<%#"Images/"+Eval("imgImagePath") %>'             runat="server" />         //... other controls     </ItemTemplate> </asp:Repeater> 

Windows 7 : IIS 8 : VS15 Community

2 Answers

Answers 1

It may be posible that you are having issues with relative URLs. You can try somehting like:

<asp:Repeater ID="rpBanquetList" runat="server"> <ItemTemplate>     //... other controls     <asp:Image ID="imgImagePath" ImageUrl='<%#"~/Images/"+Eval("imgImagePath") %>'         runat="server" />     //... other controls </ItemTemplate> 

You may have your images located in AppRootDirectory\Images\ and you are trying to access them from a page in an different directory than AppRootDirectory, lets say AppRootDirectory\MyWebform\pageWithRepeaterControl.aspx. In this case the images' relative URLs will be resolved to something like:

http://hostName/appName/MyWebForm/Images/ + the value from Eval("imgImagePath"). Note that /Images/ here, is interpreted as a subdirectory of appName.

Please take into consideration that appName may or may not be part of your project's URL. This depends on how have you deployed your web application, if it was in the root directory of your website in IIS or as an application in such a website.

You can use the ~ operator to get the path relative to the root directory. Using the same examples as before, ~/Images/ would be resolved to http://hostName/appName/Images/ which would be the AppRootDirectory\Images\ physical path in your server.

More information about web project paths here

Hope it helps.

Answers 2

You can replace asp.net controller with html controller and specify the absolute path in code behind file as follow.

<asp:Repeater ID="rpBanquetList" runat="server">     <ItemTemplate>         //... other controls          <img id ="imgImagePath" src="<%= AbsolutePath %>images/<%#Eval("imgImagePath")%>"/>         //... other controls     </ItemTemplate> </asp:Repeater> 

on code behind file

 string absolutePath =  this.Request.Url.Scheme + "://" + this.Request.Url.Host + ":" + this.Request.Url.Port + this.ResolveUrl("~/"); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment