I’m trying to make the switch from Java to .NET.
I’ve noticed a number of ASP.NET pages have
<%$ sometext %> in them. Can someone explain what this does in a couple of sentences, or point me to a reference on the syntax?
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
It’s expression builder syntax, and it’s used commonly to access settings in the
web.config. Here’s an example using expression builder syntax to get a connection string:
ConnectionString="<%$ ConnectionStrings:sqlconnection %>"
Here’s a good article that explains all of the inline expressions:
The expression builder is used to set values of control properties based on the information that is contained in an application’s configuration or resource files. The following is the basic syntax of the expression builder:
<%$ Expression Prefix: Expression Value %>
The dollar sign (
$) indicates to ASP.NET that the following expression is an expression builder. The expression prefix defines the kind of expression, such as AppSettings, ConnectionStrings, or Resources. Additionally, you can create and define your own expression builder. The expression value that follows the colon (
:) is what ASP.NET will actually use as the value of a certain property.
It references what is called an “Expression Builder”. It’s just a component that can plug into the parsing mechanism. The expression builder is fed the content of the expression, and it is responsible for returning CodeDOM expressions that describe how to get the actual value.
I’ve implemented a generic expression builder that lets you put any code in it:
It’s inline codebehind.
Here’s a link to some more info
<%$ expressionPrefix: expressionValue %> is used for expressions. Where the expressionPrefix is the expression builder it uses, and the expressionValue is the actual expression that gets passed to the expression builder.
An example usage:
<%$ AppSettings: greeting %> which would read out the
greeting from the application configuration. Various expression builders are supplied by default such as:
It is also possible to create your own custom expression builder(s).
This page gives a nice overview of various available ASP.NET tags. Although it is missing <%: %> which HTML encodes the supplied contents.
It is called expression and is used for various things including reading from web.config, app settings and resource files for localizations. Resource expressions are probably the most used form of expressions. Instead of putting the static text in the controls, this expression can be used and the ASP.NET runtime would pick the resource file for the current culture and extract the value from it.