What is this the <%$ syntax in ASP.net about? [duplicate] - asp.net

This question already has answers here:
asp.net <%$ ... %> syntax
(5 answers)
Closed 8 years ago.
For the first time I have come across this asp.net syntax:
<%$ AppSettings:ValueFromConfig %>
OR
<% $AppSettings:ValueX %>
What is this about? It's definitely not application level code because the dollar would have caused an issue. This appears in ascx and/or aspx front end pages.

I found the following:
The basic syntax of an ASP.NET expression is the following:
<%$ expressionPrefix: expressionValue %>
The dollar sign ($) indicates to ASP.NET that an expression follows.
The expression prefix defines the type of expression, such as
AppSettings, ConnectionStrings, or Resources. Following the colon (:)
is the actual expression value that ASP.NET will resolve.
Expression syntax is not bound to any specific .NET language. You can
use the same expression syntax whether you use Visual Basic, C#, or
any other programming language in your ASP.NET pages.
~ http://msdn.microsoft.com/en-us/library/d5bd1tad.aspx
This is one of the hardest questions to search for as most search engines ignore the $ sign in the question.
See also:
Introduction to ASP.NET inline expressions in the .NET Framework: http://support.microsoft.com/kb/976112

Related

ASP.NET web app is using two versions of the VB compiler - how is this happening?

I used string interpolation in a couple places - once in a VB class, and another in an aspx page. The VB class runs just fine, but if I try to run the aspx page I get a "character not expected" error on the $ symbol which denotes an interpolated string. How is this happening? Is there any way I can tell VB to use the newer version of the compiler all the time?

The $ sign in ASP.NET programming?

When does this $ sign come into the picture? It bothers me. What is its significance?
<asp:literal runat="server" text="<%$ Resources:MyResource, StringId %>" />
as seen in this thread:
Using an explicit localization expression for a page title?
Does this kind of .NET syntax have a name? I'm guessing it only works for a certain asp.net version (i.e. asp.net 3.5)? Much thanks.
It means that what comes after the dollar is an expression, see this MSDN article. It works in .net 3 & 4
The example you posted is documented here: ASP.NET Expressions Overview
It's used for expression binding, usually with localized string resources.
The $ expression binding allow us to extract custom application settings, connection string and resource information from configuration and resource files respectively.
The $ expression is a code sequence that processes the expression and replaces it with a string value in the final HTML.

asp.net <%$ ... %> syntax

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?
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:
http://support.microsoft.com/kb/976112
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:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
It's inline codebehind.
Here's a link to some more info
http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx
<%$ 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:
AppSettings
Resources
ConnectionStrings
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.

ASP.NET 4.0 DropDownList with single quotes in text

We have a asp:DropDownList that we populate server side with
ddlBranch.Items.Add(new ListItem("TEST","This is a's test"));
When this is compiled and run under .NET 3.5 we see the text "This is a's test"
However when this is compiled and run under .NET 4.0 we see the text "This is a's test"
We have added the following to our web.config and there was no change.
<pages controlRenderingCompatibilityVersion="3.5" />
For the time being we have dropped back to .NET 3.5 however we would like to know if there is a way to work around this or if this is a known rendering issue or is by design.
TIA
AJ
Hi All
Thanks for the responses and they led me to look deeper into the code looking for an Encode somewhere. It turns out there that was a:
Server.HtmlEncode(input)
being performed on all controls in a base page class.
Now what I thought was a problem really turns out to be a case of RTFM on my part
From http://www.asp.net/learn/whitepapers/aspnet4/breaking-changes
HtmlEncode and UrlEncode Now Encode Single Quotation Marks
In ASP.NET 4, the HtmlEncode and UrlEncode methods of the HttpUtility and >HttpServerUtility classes have been updated to encode the single quotation mark character >(') as follows:
The HtmlEncode method encodes instances of the single quotation mark as ' .
The UrlEncode method encodes instances of the single quotation mark as %27.
So when I was using .NET3.5 my single quote ( ' ) was being ignored by the HtmlEncode but when switching to .NET 4.0 it was not being ignored by HtmlEncode.
Thanks again for all the responses and work that people put in to this question.
Regards
AJ
When you get the value back you could just HTMLDecode the selected value.
ie. Server.HtmlDecode(ddlBranch.SelectedValue)
Why do you believe this is a problem? ' renders as an apostrophe, and when posted will turn into an apostrophe if that value is selected.

What does <%: %> do in ASP.NET (MVC2)?

A coworker recently checked in a changeset where lots of expressions on the form <%= (...) %> were changed to <%: (...) %>.
I have a vague recollection of having heard what <%: does, but cannot remember. It's too late in the night to call my coworker, and Google and Bing both seem unable to search for the string "<%:".
Can someone enlighten me, please?
It HtmlEncodes the string, if it hasn't already been encoded.
The "hasn't already been encoded part" is why MvcHtmlString was introduced. MVC2 returns MvcHtmlString from many HtmlHelper methods to represent strings that should not be re-encoded.
The <%: %> knows not to re-encode.
See What is an MvcHtmlString and when should I use it? for a good discussion.
It automatically wraps the rendered output in Html.Encode() to avoid scripting attacks.
One way to remember this is (courtesy: Scott Hanselman) think = as an closed gate. Now open the gate by turning it 90 degrees and see it from the same view. you will see :

Resources