![]() |
Web Development »
ASP.NET »
General
Beginner
License: The Code Project Open License (CPOL)
Beginner's Guide to ASP.NET Application FolderBy Abhijit JanaThis Article Describe you about Asp.Net Application folder like App_Code,App_Theme, App_Data, etc.. |
C#, Windows, ASP.NET, Dev, Design
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
First of all I would like to thanks Sean Ewington for his article Beginner's Walk for Web Development , which gives me a great opportunity to write some article for Beginner's on ASP.Net Web Development at CodeProject . After writing articles on caching and View State for beginners , I have decided to write a article on Asp.Net Application folder for Beginner's. But I will be back on rest of State management part . Like my others articles I believe this will also give you very good idea on Application Folder . Please give your valuable suggestion and improvement area that I can incorporate with this article and also on my next articles.
ASP.Net 2.0 uses file-based approach. It's
means all class files, resource files, Data , Folder are maintain in a hierarchical
way. If we are working with ASP.Net 2.0 , we can add files and folder from
"Add Items" Options. If we look at a sample application hierarchy ,
It will looks following figure.

we can add as many as files and folder (as per our requirements ) with our solutions. And it's doesn't need to recompile them each and every time when they are added. Its Asp.Net's overhead to dynamically compiled them when required. So, what ASP.Net 2.0 does, it contain a defined folder structure, contains files (Classes, Images, Resources etc.. ), compile them dynamically and even we can access those files through out the application. ASP.Net Provides some special folder also to maintain files and resources. Let's see the advantages of using those folder .
Following are the main advantages of use of ASP.Net Application Folder
Classes,
Images, Code, Databases , Themes ) in a organized way, which give us to
developed and maintain sites easily.
Asp.Net treats following Folder in a special way , those are
App_CodeBinApp_DataApp_ThemeApp_BrowserApp_WebReferenceApp_LocalResourceApp_GlobalResource Now , look at the use of those folder , I am
starting from App_Code.
As name suggest,
App_CodeFolder storeclasses,typeddatasetetc. All the items that are store in App_code , are automatically accessible by through out the application. If we store any class files (like.csor.vb) its compile them automatically . Its automatically create type data set from.Xsdfile and createXML Web services proxy classfromWSDL. Let's have a look how we can use App_Code Folder.We can add
App_CodeFolder , by Solution File -> Right Click -> Add Asp.Net Folder -> App_Code. NowApp_CodeFolder is now added with your application.
Note :-Try to Add one more App_Code Folder by using following steps. Opppps... No , App_Code folder is not available there. So , Asp.Net allows to add App_Code folder for once.Now we can add new items like Class, text, xml files inside the App_Code folder or we can also add existing files.
Let's have a look one example , that how its works . In the
App_Codefolder , I have added a class"MyCSharpClass.cs".
In that class I have written small spice of code for adding two number.
Now, Try to access this class, from any where of you application. "
MyCsharpClass" will be accessible through out the application.
If we want to store different class like
.cs, .vb
then what will happened. If we kept both
.cs and
.vb
class it will give following compilation error.

Because, All the classes contained in
App_Code
Folder are built into a single Assembly , It can't have different language in
root level or even a child level folder in following way.

We have a
solution to overcome this problems. We have to create separate
folder for
C#
and VB
or other classes.

Store class
files separately on those folder and an configure the folder
hierarchy on web.config
file .

Now , I am moving to our next Part that is Bin Folder .
Bin folder is used for keeping the assemblies
inside it. we can access those as a reference from any where of our web
application. Use of Bin folder comes to the picture if we use any class library
with our web application. Suppose, we are creating a Class Library "TestLib"
, After build the library , we will get "TestLib.dll",
Now Right Click on Solution file -> Add References -> Project , Select TestLib
Project, Click on OK. Check the bin folder, its contain
TestLib.Dll
and TestLib.Pdb
files.

Assemblies in Bin folder do not need to
registered . Asp.net recognize the presence of dll inside the Bin Folder .
Keeping .pdb files inside bin folder helps us for debugging. Main limitation of
storing assemblies in bin folder that , they are in scoped of Current
Application . Therefore they cannot access any code outside of current web
application. [Source]
Let's have a look on App_Data Folder
App_Data
Folder used as a "data storage"
for the web application. It can store, .mdf, .mdb, XML, etc files.
Its manage all of your application data in a centrally manner. It can be
accessible from any where of your web application. The real advantage is
that any file you place in App_Data won't be downloadable.
We can add .mdf file into App_Data
Folder , directly by "Add New Item"
. From there we can create table, procedure, function without opening SQL
Server. Now if we want to add that data in our application we can easily
use.

Now, look at the connection string that we need to write for accessing App_Data folder databases.

So we can connect with "MyDB.mdf"
file using this ConnectionString
. Check the bellow example which I have
used to read the Table data from "MyDB.Mdf"
file.

If you want to give your web sites a consistent
looks then you need to design Themes for your web application.
App_Themes
folder contains your Themes . A Theme folder can contain two Folder, one for
Css files
and another for Skin files.
When we will add "App_Theme"
automatically a sub folder with name "Theme1" will be created. we can change the
name of Theme folder as per our requirement.

I will not cover how to create skin file or Css file in this article, my main concern is how to apply them. You can find the details of Skin and Css over here .
Now we have to apply the theme to the page, there are several way to do that, we can set the theme from "aspx" page at page directive in following way,

While we are going to set theme from aspx
page list of Theme available to us as shown in picture. We can set the theme
from code behind file also and even we can change theme runtime (using
HttpHandler).
App_Browser
Folder contain browser information files (.browser
files). These files are xml based
files which are used to identify the browser and browser capabilities . you will
find the browser files in following location,

If you want to change .browse file, just copy
the file to App_Browser
Directory and change it. You can create new
Browser files by just click on "Add New Item
" on App_Browser Folder

As I already mention, Browser file is an configuration file, generally its look like this

AS name suggest,
App_WebReference folder contain
reference of any web services. If we added any web servies with our web
application it will come automatically inside App_WebReference folder as in
windows application if we added any ".dll"
its comes under "Reference"
Folder .

Before start about App_GlobalResource
and App_LocalResoce,
I would like to give small introduction of Asp.net Resource. If you are creating
a sites for multinational company or a public web sites that can be accessible
by all over the world, you
need to consider the best ways to address users in different cultures with
different languages with different country.ASP.NET
provides the infrastructure to create Web applications that automatically adjust
formatting and languages according to user preferences using Resource files.
Main purpose of Resource files is "Localization"
of web application.
ASP.NET uses resource files to make supporting multiple languages in a simpler way. Visual Studio 2005 can automatically generate XML resource files that contain text for your controls in different languages. When user visit the sites they can change the languages of the sites based on there preference. There are two type of resource
Local ResourceGlobal ResourceApp_LocalResource
folder contain Local Resources files and
App_GlobalResource Folder contain
Global Resources files.
Local Resource :-
Local
resources are specific to a single Web page and should be used for providing
multilingual functionality on a Web page.
Local resources must be stored in an App_LocalResources subfolder of the folder containing the Web page. Because you might have local resources for every page in your Web application, you might have App_LocalResources subfolders in every folder.
Resource file name should be like
<pageName>[.langauge].resx
. some example of local resource files are, Mypage.aspx.ex.resx,
Mypage.aspx.de.resx. Default.aspx.resx is the base resource file to
use if no other resource file does not match with user current culture.
if you want to create local resource for a page , open the design view of the page and then from "Tool Menu" -> "Generate Local Resource" . The you will see a resource file is automatically created on App_LocalResource Folder.

Following code showing you
XML Code for the resource file. for
changing Resource file we can change from xml or Resource Editor also.

If we changing any thing on the page
resource file will automatically updated. If want to test application for
Local_Resource,
Just create a copy of "Default2.aspx.resx"
and renamed it to "default2.aspx.fr.resx
( for french ). Now for a test, I have changed the button tool tip for
French Resource to "French ToolTip". Now Change your IE setting and set "French
as default Culture" from Tools -> Internet options -> Languages , Add
French in the list and test the Application.

Test the Button Tool Tip and you can test it for other Language also.

Global resources
can be read from any page or code that is in the Web site . Global resources
must be stored in the App_GlobalResources
folder at the root of the
application. We should use App_GlobalResource
folder when we need single resource from multiple web pages. we can define
Asp.net control properties by manually associating them with resources in global
resource files. you can add global resource file by right click on "App_GlobalResources"
folder and click on "Add Items". Add .resx files as resource.

We can access those resources by
programmatically .by Resource.Resource
Object.
e.g :
Label1.Text= Resource.Resource.MyText
We can use the GetLocalResourceObject and GetGlobalResourceObject methods to access global resources, and then cast them to the correct type .
Label1.Text = GetLocalResourceObject("Label1.Text").ToString();
As I already told this article all about the use of all application folder that are available in asp.net. I hope I have describe them well. There are lots of thing to learn like how to design themes and Skin, Working with Resource files . I am giving some reference links that can help you for further study. Skins & Themes , Asp.Net Resources .Please Read those link at least for Resources which is very important part for Asp.Net 2.0 . In future I can plan for writing an article for beginner's on Resources. But Not now.
Again Thanks for reading this article. Please don't forget to give your suggestion .
Written on 07-Dec-2008
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 23 Dec 2008 Editor: |
Copyright 2008 by Abhijit Jana Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |