Neudesic Blogs

Think Smart. Get Results.
Welcome to Neudesic Blogs Sign in | Join | Help
in Search

Pete Orologas

Lost Treasure of Asp.net 2.0

$ 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

Published Wednesday, April 19, 2006 1:57 AM by Porologas

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Austin Bailey said:

Wow Pete! That looks really cool! I'll have to remember that for my next ASP.NET project!
April 20, 2006 5:04 PM

What do you think?

(required) 
(optional)
(required) 

This Blog

Post Calendar

<April 2006>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

Syndication

Powered by Community Server, by Telligent Systems