The biggest trouble in Web programming has been the state management due to HTML’s statelessness. MS’s VB(Visual Basic) became the one of the most successful programming languages because it supported RAD(Rapid Application Development). You can draw a control (such as a button) from a tool bar, double-click it, and write some code. Viola! It’s done.
Microsoft wanted to expand the RAD enviornment to the web programming, which had been the weeker point of MS. So RAD is adopted into .NET Web applications. The result was the Web Form.
Web Forms became really popular recent years. Now ASP.NET MVC becomes the developers’ choice for a new application. But there are tons of sites developed using Web Forms out there. As a Web developer, it is required to understand both Web Forms and MVC applications.
1. ASP.NET Web Form Application
Web Form applications hide (encapsulate) the HTML as much as possible. (MVC proponents think it can be bad too) Windows developers can be web developer pretty easily because of their similarity of programming styles.
The features of a Web Form applications are:
- Each application is executed in a separate application domain
- Web Form applications consist of web pages(.aspx and .html), files(images and styles), handlers, modules, executable code (dlls), and configuration files (web.config).
2. ASP.NET File Types
- aspx: web pages
- ascx: user contols
- web.config: the xml-based configuration file
- global.asax: the global application file in which you can define global variabnles and handle global events
- cs, vb: code-behine files
3. ASP.NET directories
- App_Browsers: contains “.browser” files that ASP.NET uses to identify browser capabilities
- App_Code: contains source code files that are dynamically compiled (.cs, .vb)
- App_Data: stores database files or xml files as your data source (.mdf, .xml)
- App_GlobalResources: contains global resource files that are accessible to every page in the application (.resx, .resources)
- App_LocalResources: contains resource files that are aceessible to a specific page, or a user control only (.resx, .resources)
- App_Themes: contains subfolders that each define a specific theme (.skin, .css …)
- App_WebReferences: stores references to web services (.wsdl, .xsd, .disco …)
- Bin: contains all the compiled DLLs
You can do the following tasks in the global.asax.
- You can add the golbal.asax file using the menu (“Add New Item” -> “Global Application Class”) if the file does not already exist
- Writes handlers for global (application-level) events
– Application Events –
– Application Events related with page requests –
5. Web Configuration Files
Web sites are configured based on a hierarchy of XML configuration files, starting with the “Machine.config” file (%SystemRoot%\Microsoft.NET\Framework\<version>\CONFIG\), followed by “Web.config” file in the same folder. After that, you might have a “Web.config” file in the root of the web site, and in any subdirectory.
<configuration> <appSettings> <add key ="..." value="..." /> </appSettings> <connectionStrings> <add name="..." connectionString="..." providerName="..." /> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0" /> ... </system.web> </configuration>
Configuration files can be edited with a text editor, an XML editor, the Configuration snap-in for MMC (Microsoft Management Console), or the WSAT (Web Site Administration Tool).
The WAST (“Website | ASP.NET Configuration” menu) is used to add and modify the web site settings.
- Security: users / roles / permissions
- Application: Application settings / SMTP settings / Debug and Trace
- Provider: DB provider for maintaining membership and roles
6. Storing Custom Settings in the Web.config File
You can add settings in the “appSettings” section in the “Web.config” file. By storing settings in the configuration file, you can centralize the location of settings and also easily modify the settings.
When you want to read custom settings in your code, you can use the “System.Web.Configuration.WebConfigurationManager” class.
<appSettings> <add key ="LogFileExtension" value="log" /> </appSettings>
string logFileExtension = WebConfigurationManager.AppSettings["LogFileExtension"];
or you can read a value in the aspx page
<p>log file extension = <%= System.Web.Configuration.WebConfigurationManager.AppSettings["LogFileExtension"]%> </p>
7. Storing Connection Strings in the Web.config File
When you work with DB in your web application, the location of DB should be configured without compiling the code again. Therefore the Web.config file is the right place for DB connection strings.
DB Connections are stored in the <connectionStrings> section and the “System.Web.Configuration.WebConfigurationManager” class is also used to read the connection strings.
<connectionStrings> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> </connectionStrings>
When you access a connection string using the “WebConfigurationManager.ConnectionStrings[“name”], the result is not just a string, but is an object of “System.Configuration.ConnectionStringSettings“.
string dbConStr = WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; string dbConProvider = WebConfigurationManager.ConnectionStrings["ApplicationServices"].ProviderName;