ASP.NET Basics Part two – special folders
by Mats Hellman on 11.Feb, 2009 under ASP.NET, Programing
I’m really sorry this post is a little late. My keyboard’s left shift button died and I had to get myself a new one. More on that in another post.
I assume you have read the article from last week. This week we are going to take a look at a few of the special folders that ASP.NET 2.0 uses, in specific we will use the folders APP_CODE and APP_LOCALRESOURCES. There are also a few others but I’ll get into them when I start using them myself
.
Special folders are new to ASP.NET 2.0 and the idea of using them is plain brilliant. Any class, wsdl file or typed datasheet you put in APP_CODE will automatically be available for all pages in your web application. And with APP_LOCALRESOURCES you can set the language of the page by using a few files. This really is brilliant and it make development faster, so you can spend more time in the all important testing phase .
The Demo2 website
After you start up your development IDE, be it Visual Webdeveloper or Visual Studio, create a new website and call it demo2. When you have your new solution open you can delete the APP_DATA since we won’t be using it today.
After that right-click on the URL to your solution and select Add ASP.NET Folder then create one APP_CODE folder and one APP_LOCALRESOURCES folder.
Next let’s get on with some examples on what we can do with our folders.
APP_CODE folder
As I said earlier this folder can hold classes, .wsdl (Web Service Description Language) files and typed datasheets and any of them placed in the folder automatically becomes available for the rest of the pages in your solution. You can even place class code from different languages, like VB.NET and CSharp. The only thing to remember if you decide to use more than one language is that they should be separated in subfolders. Using VB and CS would be the logical choice. If you want to use both languages you have to do a few changes. First of all you have to ad a web.config file to your solution and in it place the following simple code
<compilation> <codesubdirectories> <add directoryname="VB"></add> <add directoryname="CS"></add> </codesubdirectories> </compilation>
This is just to tell ASP.NET compiler where to find the code. You can’t throw both .vb and .cs files in the same directory. And this way it is really clear where to find them.
To demonstrate the use of the folder we will make a simple calculator class. So ad a Class file in the APP_CODE folder and call it Calculator.cs.
public class Calculator { public int Add(int a, int b) { return (a + b); } public int Sub(int a, int b) { return (a - b); } }
After opening the calculator class we add some logic. This will be a really simple application so it wont do much but we could still have two functions, Add and Sub. I will only use Add in this demo but this gives you a view into the logic here.
What you should notice is that as soon as you have the code in place and save it Intellisense picks it up and you can start using it in the Default.aspx file. Or as I did in Default.aspx.cs. This is a really simple page and all it does is add two values which are hardcoded in the page. I chose 100 and 100 and add them together. And voila it works.
APP_LOCALRESOURCES
The localresources folder is used to construct resources that can be used by a single .aspx file, the globalresources does almost the same but it is application-wide. So to test this feature lets create a page called, imaginatively, Test.aspx. Leave it empty for now. Add two resource files in localresources, I chose to add the default Test.aspx.resx and a file for my native language Swedish, Test.aspx.sv.resx. In them ad the following
Test.aspx.resx
| Name | Value | Comment |
| PageTitle | Welcome to Yoursite :: Demo2 | |
| Question | What’s your name? | |
| Answer | Hi there, |
Test.aspx.sv.resx
| Name | Value | Comment |
| PageTitle | Välkommen till Dinsida :: Demo2 | |
| Question | Vad heter du? | |
| Answer | Hej på dej, |
You can use your own languages, just change sv to anything you want. You can also have many many more files in here if you want. I only speak and write Finnish,Swedish and English so my site would be quite small. But if you write 20 languages feel free to put them all in there
.
Now let’s go back to the Test.aspx file. We need to do some work on it to get the local resources in use. First of all, drop in a label (yes I like them) and a textbox and a submit button and another label.
Call the above lblQuestion, tbName and lblAnswer. Remember to keep ID’s as clear and descriptive as possible. It’s not a problem in a solution like this one but if you are working on something with say 1000 pages it will be. Now open the codebehind file and paste or write the following.
public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Page.Title = (String)GetLocalResourceObject("PageTitle"); lblQuestion.Text = (String)GetLocalResourceObject("Question"); } protected void Button1_Click(object sender, EventArgs e) { lblAnswer.Text = (String)GetLocalResourceObject("Answer") + " " + tbName.Text; } }
And here is a screenshot of the application in action. My browser is set to Swedish locales so my page displays in Swedish.
![]()
Se how easy it was to get the values out of our resource files. As I told you there is nothing to it. It’s fast, simple and well for someone who likes code simply beautiful. As always the code for this application can be downloaded as a .zip file.

