$ expressions are simple code sequences that can be added to any aspx page as long as they are within a control tag. The expressions are evaluated by an expression builder and then returned to the control for display. The builder translates the expression to a string and returns it to the page when it is rendered. You will find that asp.net includes some built-in expression builders but you can also extend the ExpressionBuilder class to create your own custom expression builders. Below you will find an example of both Built-In and Custom Expression Builders.
Built-In
Three built-in expression builders are the AppSettingsExpressionBuilder, ResourceExpressionBuilder and the ConnectionStringExpressionBuilder.
AppSettingsExpressionBuilder:
Reads from appSettings in web.config
<asp:Literal runat="server" text="<%$ AppSettings:Key %>" > -- (returns "Neudesic")
--------------- web config -------------------
<appSettings>
<add key="Key" value="Neudesic"/>
</appSettings>
-----------------------------------------------
ConnectionStringExpressionBuilder:
Reads from connectionStrings section of web.config.
<asp:Literal runat="server" text="<%$ ConnectionStrings:DbConn%>" > -- (Returns ConnectionString shown below):
--------------- web config -------------------
<connectionStrings>
<add name="DbConn" connectionString="DataSource=localhost;Integrated Security=SSPI;InitialCatalog=Northwind;" providerName="System.Data.SqlClient" />
</connectionStrings>
-----------------------------------------------
ResourceExpressionBuilder
Reads from resource file.
Lets say we have a resource file with the following data in a resource file named "ResFileName.resx":
<data name="Resource">
<value xml:space="preserve">Hello!</value>
</data>
Here is where we make a call using the ResourceExpressionBuilder to retrieve the value "Resource" from our "ResFileName.resx".
<%$ Resources: ResxFileName, Resource %> == <% {ExpressionBuilder}: {ResourceFile},{ResourceKey} %>
Custom
You must register your custom expersion builders so that Asp.Net will know which class to instantiate when your custom expression is encountered. To register a custom expression builder you will add a <expressionBuilders> section into the <complilation> section of your your web.config
<!-- From Web.config -->
<compilation>
<expressionBuilders>
<add expressionPrefix="VersionNumber" type="VersionExpressionBuilder"/>
</expressionBuilders>
</compilation>
To display your custom expression builder you simply add it to a control tag. This is the same method we used with the Built-In expression builders.
<%@ Page Language="C#" CompileWith="Version.aspx.cs"
ClassName="Version_aspx" %>
<html>
<body>
<h1>Powered by ASP.NET
<asp:Literal Text='<%$ Version:MajorMinor %> or <%$ Version:All %>' Runat="server" /> or
</h1>
</body>
</html>
Code For expression builder. The code is fairly self explanatory. We inherit from ExpressionBuilder and over ride GetCodeExpression. The BoundPropertyEntry is the expression after the ":" in our example Version:MajorMinor it's the "MajorMinor". After we have our EntryExpression we can evaluate it and perform whatever operation we want.
public class VersionExpressionBuilder : ExpressionBuilder
{
public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{
string param = entry.Expression;
switch(param)
{
case "All":
Return(new CodePrimitiveExpression (***represent an expression that indicates a primitive data type value***)
(String.Format("{0}.{1}.{2}.{3}",
Environment.Version.Major,
Environment.Version.Minor,
Environment.Version.Build,
Environment.Version.Revision));
Break;
case "MajorMinor":
return new CodePrimitiveExpression
(String.Format("{0}.{1}",
Environment.Version.Major,
Environment.Version.Minor));
Break;
default:
throw new InvalidOperationException
("Use $ Version:All or $ Version:MajorMinor");
Break;
}
}
}
To read more on CustomExpressionBuilder you can visit the "Five Undiscovered Features on ASP.NET 2.0" on MSDN.
Hope this blog was informative. All feedback is Welcome.
Pete Orologas
Pete.Orologas@Neudesic.com