Professional Documents
Culture Documents
C Session 14
C Session 14
Objective
Configuration as ASP.NET Application Debugging ASP.NET Application Identifying ASP.NET Application Debugging Methods Handling Internet Related Exceptions
Configuration Files
ASP.NET configuration data is stored in two primary XML-based files. These files allow you to easily edit configuration data at any moment even after the application is deployed on server.
Machine.config: Server or machine-wide configuration file Web.config: Application configuration files which deal with a single application
Every ASP.NET server installation includes a configuration file named machine.config, and this file is installed as a part of .NET Framework installation. You can find machine.config in C:\<Windows>\Microsoft.NET\Framework\\Config\
Each and Every ASP.NET application has its own copy of configuration settings stored in a file called Web.config. If the web application spans multiple folders, each sub folder has its own Web.config file that inherits or overrides the parent's file settings.
Connection Strings
ASP.NET 2.0 introduces a new section called <connectionStrings> that stores all kinds of connection-string information. <configuration> <connectionStrings> <add name ="CookieDemo" connectionString ="server=aras02;database=aras02_Db; uid=freelance91;pwd=freelance91"/> </connectionStrings> </configuration/>
Session State
Session information using the <sessionState> element It can be used to persist state in any permanent store like XML or databases
sessionState mode ="StateServer" cookieless ="false" timeout ="20" stateConnectionString="tcpip=aras02:42424" stateNetworkTimeout="60" sqlConnectionString ="" />
Custom Errors
When the ASP.NET application fails, the ASP.NET page can show the default error page with the source code and line number. We can prevent this kind of error messages by configuring <customErrors> element which allows for defining custom error messages in an ASP.NET application. <customErrors mode="[on/off/RemoteOnly]" defaultRedirect="[URL]"> <error statusCode="[statusCode]" redirect="[URL]" />
Debugging An Application
Debugging is the process of finding and fixing errors in your application Two types of errors:
Compile time errors: these are largely syntactic errors which will be captured by the compiler. Note, that use of Option Explicit and Option Strict can reduce the likelihood of runtime errors via compile time identification of likely issues. See my article on Error Handling in ASP.NET for more information. Runtime errors: these are bugs programs that compile successfully but do not behave as expected.
Debug Mode
Debug mode may be set explicitly within the web.config file
<compilation debug="true" />
Stepping is the step-by-step execution of a program The steps of the stepping process are definable by the developer. The debug menu of VS.NET provides three options for step execution: Step Into: executes the code in step mode if a method call is encountered the program execution steps into the code in step mode. Step Over: will not execute the next method call in step mode, continuing to the next statement after the method call. Step Out: if already in a method call will continue through the rest of the method without stepping, returning to step mode when control returns to the calling statement.
Watch
The Watch window allows you to add 'watches' to particularly variables and expression for continuous monitoring. Thus when you set up a watch whenever you return to the IDE while debugging you will see the current value of the variable or expression defined by the watch. Autos The autos window (automatically) displays the variables in the current statement and the previous statement. Locals Displays the variables local to the current context, i.e. the current method under execution Me The Me window allows you to examine members associated with the current object typically a web form for an ASP.NET application, but not necessarily. It also allows you to change these values. Immediate The immediate window allows (immediate) access to the values of variables and expressions while debugging, at the breakpoint. Call Stack The Call Stack window shows you the method call stack thus providing information about the path taken by the code to reach its current point of execution
Turning debugging on and off at the page level is easy enough when you're making changes to only a few pages You can activate Debug mode at the application level by setting the Debug attribute of the compilation section in Web.config
<configuration> <system.web> <customErrors mode="Off" /> <compilation defaultLanguage="C#" debug="true" numRecompilesBeforeAppRestart="15"> </compilation> </system.web> </configuration>
Tracing
Tracing is a way to monitor the execution of your ASP.NET application. You can record exception details and program flow in a way that doesn't affect the program's output.
Application Tracing
You can enable tracing for the entire application by adding tracing settings in web.config.
<configuration> <appSettings/> <connectionStrings/> <system.web> <compilation debug="false" /> <authentication mode="Windows" /> <trace enabled ="true" pageOutput ="false" requestLimit ="20" traceMode ="SortByTime " /> </system.web> </configuration> In Above example, pageOutput="false" and requestLimit="20" are used, so
Tracing can be viewed for multiple page requests at the application level by requesting a special page called trace.axd
<system.web> <compilation debug="false" /> <authentication mode="Windows" /> <trace enabled ="true" pageOutput ="true" /> </system.web>
protected void Page_Load(object sender, EventArgs e) { System.Diagnostics.Trace.Write("This is Page_Load method!");