3

I would like to use ASP.NET's ExpressionBuilder syntax to dynamically retrieve domain of static content from an AppSetting.

I am using the following syntax, which does not work:

<img src="<%$Appsettings:STATIC_CONTENT_DOMAIN %>/img/logo.jpg" alt="logo" width="176" height="159" />

FYI, the desired HTML output is:

<img src="http://static.myserver.com/img/logo.jpg" alt="logo" width="176" height="159" />

Please note, I cannot use <%= %> syntax because my ASPX page needs to be CompilationMode="never". (The reason I am using ExpressionBuilder syntax is that it works in no-compile pages)

Any ideas on how I can do this?

frankadelic
  • 20,543
  • 37
  • 111
  • 164

3 Answers3

5

This approach worked for me (not very readable :)...

<img src="<asp:Literal runat='server' Text='<%$Appsettings:STATIC_CONTENT_DOMAIN%>'/>/img/logo.jpg" />
frankadelic
  • 20,543
  • 37
  • 111
  • 164
2

You might want to consider writing a custom expression builder - they're not too difficult to write. Here are some tutorials:

You could have your own expression syntax such as:

<%$ MyCdnUrl: Static, '/img/logo.jpg' %>

Then you'd parse out everything after the ":" and build up the URL that you need.

I think that expression builders must be used as "property values" so you can't use them completely on their own. You'll still have to use something like <img runat="server"> or an <asp:Image> control or an <img> with the <asp:Literal> inside it.

Eilon
  • 25,582
  • 3
  • 84
  • 102
  • This is a much cleaner syntax than my answer. I'll try it out. – frankadelic Jan 29 '10 at 17:27
  • It appears I need to use runat="server" on the tag. The problem here is that I would also like to use this with client side script references, such as . Unfortunately, that will not work with runat="server". – frankadelic Jan 29 '10 at 20:46
  • @frank can you clarify why having runat="server" won't work? It still emits a regular `` tag that you can access from JavaScript code. – Eilon Jan 29 '10 at 22:57
  • Sorry, to clarify: I was saying, will not work in my case. It is interpreted as server-side code. I didn't mention in my original question, but I would like to use this with tags, scripts, and css references. – frankadelic Jan 29 '10 at 23:21
  • @frank then unfortunately this just might not be a good usage for expression builders. You can still do something like what your upvoted answer shows combined with my suggestion of a custom expression builder. – Eilon Jan 30 '10 at 02:57
0

I believe you need to use a server-side asp.net control, such as:

<asp:Image ID="MyImage" runat="server" ImageUrl="<%$Appsettings:STATIC_CONTENT_DOMAIN %>" />

I don't know if you can combine the statement with static info like you have, such as:

<asp:Image ID="MyImage" runat="server" ImageUrl="<%$Appsettings:STATIC_CONTENT_DOMAIN %>/img/logo.jpg" />

My guess would be it isn't possible, but I guess it's worth a shot. Try it out and see...

KP.
  • 13,218
  • 3
  • 40
  • 60
  • Here's a good similar discussion as well: http://stackoverflow.com/questions/1681892/appsettings-in-markup-issue – KP. Jan 27 '10 at 19:05
  • Tried your second example. It does not work with static content after the expression block. – frankadelic Jan 27 '10 at 19:14
  • Yeah I didn't think it would. You're likely going to be pretty handcuffed by not being able to compile the pages, with respect to flexibility. – KP. Jan 27 '10 at 19:42