views:

53

answers:

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?

A: 

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
Here's a good similar discussion as well: http://stackoverflow.com/questions/1681892/appsettings-in-markup-issue
KP
Tried your second example. It does not work with static content after the expression block.
frankadelic
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
+1  A: 

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

<img src="<asp:Literal runat='server' Text='<%$Appsettings:STATIC_CONTENT_DOMAIN%>'/>/img/logo.jpg" />
frankadelic
A: 

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
This is a much cleaner syntax than my answer. I'll try it out.
frankadelic
It appears I need to use runat="server" on the <img> tag.The problem here is that I would also like to use this with client side script references, such as <script type='text/javascript' src='....'></script>. Unfortunately, that will not work with runat="server".
frankadelic
@frank can you clarify why having runat="server" won't work? It still emits a regular `<img>` tag that you can access from JavaScript code.
Eilon
Sorry, to clarify: I was saying, <script runat="server" type='text/javascript' src='<%$ MyCdnUrl: Static, "/js/lib.js" %>' /> 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 <img> tags, scripts, and css references.
frankadelic
@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