0

I have a web.config key :

<add key="IMGSRC" value="http://localhost" />

I want to use the value of this key along with the path of the image concatenated to in an aspx page. I'm aware that I can get to root folder by simply saying "../ImagesFolder" , but my website has parent path disabled because of security concerns. So now I need to work around it.

I need something like this (Here are a few things I tried after looking up the internet and which did not work.):

1) <img id="Img19" runat="server" alt="Admin" src='<%#ConfigurationSettings.AppSettings["IMGSCR"] %>' />

2) <img id="Img19" runat="server" alt="Admin" src='<%#ConfigurationSettings.AppSettings["IMGSCR"] + "/ImagesFolder/img.jpeg" %> ' />

3) <img id="Img19" runat="server" alt="Admin" src="<%#ConfigurationSettings.AppSettings["IMGSCR"] %> " + "/ImagesFolder/img.jpeg" />

Also I tried this: I declared a variable Path on Page_Load Path = System.Configuration.ConfigurationManager.AppSettings["RootforIMG"].ToString();

and then on aspx page I tried using it as <img id="Img19" runat="server" alt="Admin" src="<%=Path %> " + "/ImagesFolder/img.jpeg" /> but this as well is no good.

3
  • Take a look on this post here on SO. Commented Sep 12, 2012 at 13:19
  • @Adriano In my case I have a lot of small images on a single page.I cannot make so many variables in codebehind by mapping each image with server.mappath(). Commented Sep 12, 2012 at 13:24
  • you do not need to create variables. Just use Server.MapPath() inside your page instead of reading path from configuration. Commented Sep 12, 2012 at 13:52

3 Answers 3

0

Can you try something like below?

<img id="Img19" runat="server" alt="Admin" src='<%= GetImageSource()%>' />

In code behind

public string GetImageSource()
{
 return ConfigurationManager.AppSettings["IMGSCR"] + "/ImagesFolder/img.jpeg";
}
Sign up to request clarification or add additional context in comments.

6 Comments

For finding application path,use this.. public string CurrentAppPath() {return (System.Web.HttpRuntime.AppDomainAppVirtualPath == "/") ? string.Empty : System.Web.HttpRuntime.AppDomainAppVirtualPath; }
I would pass the "/ImagesFolder/img.jpeg" into the GetImageSource() function, otherwise you're doing way too much work.
What if I have a lot of images on a single page.I will have to make that many variables in code behind.
Better you call like src='<%= CurrentAppPath()+'imagefile'%>'
Both the methods tried, with <%= %> , it gives me a not "well formed" error, whereas with src='<%# CurrentAppPath()+'imagefile'%>' , it does not return a string.I mean when I view source, I see src="". Also I have a breakpoint on the method which does not get called.
|
0

The relative path of images should work:

Relative path can be as per your page location i.e: '../IMages/img.jpg' or 'images/img.jpg'

try this :

<img id="Img19" runat="server" alt="Admin" src='ImagesFolder/img.jpeg' />

Comments

0

For controls with runat="server" attribute you do not need any special code to map a path relative to web site root:

<img id="Img19" runat="server" alt="Admin" src"~/ImagesFolder/img.jpeg" />

The path ~/ImagesFolder/img.jpeg will be resolved replacing ~ with the root folder of your web-site.

If to resolve path you need some kind of logic (for example you need to call a function) then you can use this:

<img src'<%= ResolveImageName() %>' />

Do not forget that URL must be proper encoded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.