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

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.

Related

Regular Expression Extractor failing in JMeter for asp.net web forms

Using JMeter, I'm attempting to log in to an asp.net web forms application. I recorded the login sequence to a *.jmx file, and now I'm attempting to extract the __VIEWSTATE, __VIEWSTATEGENERATOR and __EVENTVALIDATION hidden inputs using the Regular Expression Extractor post-processor.
In all 3 cases JMeter is extracting the name of the variable that I want to extract into (eg "${viewstate}") instead of the value I want to extract. Here is what the RequestBody looks like when I look at the ViewResults Tree and select "Text":
ReturnUrl=%2F&__VIEWSTATEGENERATOR=%24%7Bviewstategenerator%7D&__EVENTARGUMENT=&__VIEWSTATE=%24%7Bviewstate%7D&ctl00%24ContentPlaceHolder1%24Login1%24LoginButton.x=25&ctl00%24ContentPlaceHolder1%24Login1%24Password=MyPassword%21&ctl00%24ContentPlaceHolder1%24Login1%24LoginButton.y=4&__LASTFOCUS=&ctl00%24ContentPlaceHolder1%24Login1%24UserName=MyUserName&__EVENTTARGET=&__EVENTVALIDATION=%24%7Beventvalidation%7D
Oddly enough, if I select "RegExpTester" in the ViewResults Tree and test my regular expressions, all of them appear to work.
For example, here is what my __VIEWSTATE extractor looks like:
The Regular Expression is this bit of text:
name="__VIEWSTATE" id="__VIEWSTATE" value="(.+?)"
When I enter that expression into the RegExp Tester it finds it. The other 2 also work:
This is my first time using JMeter, I suspect I've got something in the wrong place.
Here is how my HTTP Request is set up:
Here is how the entire project looks:
Where do you expect these values to come from? You're missing one GET request which will open the login page, your test should not start from POST request.
Once you execute GET request - your Regular Expression extractors will capture viewstate and friends and you will be able to log in.
Also consider switching to CSS Selector Extractors as using regular expressions to parse HTML is not the best idea.
The relevant CSS Selector expression would be as simple as input[id=__VIEWSTATE], use value as the attribute. Similarly correlate remaining dynamic values. See ASP.NET Login Testing with JMeter article for more details if needed.

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

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

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.

What does 'parse-time' mean? Does it mean rendering?

When I was reading Asp.Net Expressions article, I have seen such a sentence:
"Expressions are evaluated at run time when the declarative elements of the page are parsed, and the value represented by the expression is substituted for the expression syntax. (Because expressions are evaluated at parse time, you cannot dynamically create expressions in code.)"
Where does the parse-time exist in terms of page life-cyle? Rendering?
Yeah, the "code front" or the "aspx" page is interpretted each time a request for it is made. At the time of this request the aspx page is consumed or parsed by the asp.net dll. This would be the point in time they are referencing in this blurb.

asp.net: using resource expressions with the resources in a separate project

I'm pretty sure this is something super easy, but how do i access a resource that is in a separate project using the expression syntax?
I thought it would be like so:
<%$ Resources:SomeNamespace.Resources.Web, PleaseSelectAnImage %>
where SomeNamespace.Resources is the project that the resources are located in.
i normally just do <%= SomeNamespace.Resources.Web.PleaseSelectAnImage%> but i need to have this inside a control.
<kw:SlickUpload ID="SlickUpload1" runat="server"
ValidExtensions=".png, .gif, .jpg"
InvalidExtensionMessage="<%$ Resources:SomeNamespace.Resources.Resources.Web, PleaseSelectAnImage %>" >
so when i just do what i normally do, it puts
'<%= SomeNamespace.Resources.Web.PleaseSelectAnImage%>" literally.
my project name is SomeNamespace.Resources. the resource file name is Web. and the key is PleaseSelectAnImage.
Patricia, you'll need to implement a custom ResourceProviderFactory and corresponding IResourceProvider in order to do this. Some good reference can be found here.

Resources