Create Custom Expression Builders in ASP.NET - More ASP.NET Extensibility
by David Hayden ( Florida .NET Developer ), Filed: ASP.NET 2.0
I need the concept of Expression Builders in an application, which led me to thinking about custom Expression Builders that were introduced in ASP.NET 2.0. Per the documentation:
“ExpressionBuilders in ASP.NET 2.0 are a parse-time feature that allows the page developer to add declarative syntax to assign to a control's properties. ASP.NET 2.0 ships with expression builders for:
- Connection strings. Used to access connection-strings in configuration. <%$ connectionstrings: MyConnStr %>;
- Application settings. Used to access the application setting in configuration. <%$ appsettings: MyAppValue %>, and;
- Resources. Used as the foundation for building a multi-lingual site. <%$ resources: mykey %>.“
You have probably used the ConnectionStringsExpressionBuilder in an SqlDataSource control to avoid hardcoding the connection string in your ASP.NET Page:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT * FROM [products]">
</asp:SqlDataSource>
You can build custom Expression Builders quite easily and use them in your applications.
First things first, you need to create a custom Expression Builder. Here is one I created quickly just now that gets information from HttpContext.Items, which is a key-value collection that can be used to organize and share data during an HTTP request. A lot of stuff in there that is basically boiler plate code that can be used over and over.
namespace PnPGuidance.CustomerExpressionBuilders
{
[ExpressionPrefix("ContextItem")]
public class ContextItemExpressionBuilder
: ExpressionBuilder
{
public override CodeExpression GetCodeExpression
(BoundPropertyEntry entry, object parsedData,
ExpressionBuilderContext context)
{
return new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression(
base.GetType()),
"GetContextItem",
new CodeExpression[]
{ new CodePrimitiveExpression(parsedData) });
}
public static object GetContextItem(string expression)
{
if (HttpContext.Current == null)
return string.Empty;
return HttpContext.Current.Items[expression];
}
public override object ParseExpression(string expression,
Type propertyType, ExpressionBuilderContext context)
{
return expression;
}
}
}
The key is the GetContextItem method which returns the value in HttpContext.Items based on a particular key ( expression ) that is provided by the ParseExpression method.
You can use the ContextItemExpressionBuilder in your page as follows:
<asp:Label
ID="MyLabel"
Text="<%$ ContextItem: Key %>"
runat="server" />
and the value of HttpContext.Current.Items[“Key“] in this case will be displayed as the text value of the label.
You have to register your Expression Builder in the web.config file so the compiler can find it:
<compilation debug="false">
<expressionBuilders>
<add expressionPrefix="ContextItem"
type="....ContextItemExpressionBuilder,
PnPGuidance.CustomerExpressionBuilders"/>
</expressionBuilders>
</compilation>
Cool stuff!
Author: David Hayden ( Florida .NET Developer ), Filed: ASP.NET 2.0