Use of Web.Config File.
How many ways you can use it.
Web.Config is xml based text file.It is derived from Machine.config file. It means whatever defined/written in Machine.Config file will be overwritten by Web.Config file.
Same way you can have multiple Web.Config files in same ASP.Net Wep Project. According to folder structure what ever written in parent folder will be overwritten by child folder Web.Config file.
Grate functionality of Web.config file is it does not required iis-restart or application restart. Next request will use updated web.config file. In traditional ASP project developers need to restart iis.
Example : You can have multiple configuration file in one web application in different folders to give different kind of authorization to different users.
Consider a scernario you have one website, inside that you have 5 resourses(usually aspx pages) as below
1. welcome .aspx
2.usermanagement.aspx
3.home.aspx
4.courses.aspx
5.facultyDetails.aspx
in this case you would like to give the access of
usermanagement.aspx to only admin users
courses.aspx,facultyDetails.aspx to only registerd users
welcome .aspx,home.aspx to all users
so inorder to acheive this ,what we will do is
we will create 2 folders like
1.Admin
2.RegisteredUSer
then we need to place usermanagement.aspx in admin folder as add a web.config in this folder and in that file we can authorize the only admin users.
We need place courses.aspx,facultyDetails.aspx in Registered User folder as add a web.config in this folder and in that file we can authorize the only registered users.
rest of the pages we can place in root folder itself.
1. welcome .aspx
2.usermanagement.aspx
3.home.aspx
4.courses.aspx
5.facultyDetails.aspx
in this case you would like to give the access of
usermanagement.aspx to only admin users
courses.aspx,facultyDetails.aspx to only registerd users
welcome .aspx,home.aspx to all users
so inorder to acheive this ,what we will do is
we will create 2 folders like
1.Admin
2.RegisteredUSer
then we need to place usermanagement.aspx in admin folder as add a web.config in this folder and in that file we can authorize the only admin users.
We need place courses.aspx,facultyDetails.aspx in Registered User folder as add a web.config in this folder and in that file we can authorize the only registered users.
rest of the pages we can place in root folder itself.
Ø module loading,
Ø security configuration,
Ø application language,
Ø compilation settings,
<system.web
<compilation debug="true" strict="true" explicit="true" batch="true" optimizeCompilations="true" batchTimeout="900" maxBatchSize="1000" maxBatchGeneratedFileSize="1000" numRecompilesBeforeAppRestart="15" defaultLanguage="c#" targetFramework="4.0" assemblyPostProcessorType=""
>
<assemblies>
<add assembly="System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</assemblies>
</compilation>
</system.web>
Ø Web.config files can also contain application specific items such as database connection strings.
Ø Error redirect
i.e.
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
<configuration/>
<system.web>
<customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
</system.web>
Ø Page Level Settings
i.e.
<system.web>
<pages enableViewStateMac="true" enableEventValidation="true" viewStateEncryptionMode="Always">
<system.web>
Or
<system.web>
<pages buffer ="true" styleSheetTheme="" theme ="Acqua”
masterPageFile ="MasterPage.master"
enableEventValidation="true">
<system.web>
Ø Location Sections
<location path="Login.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path ="Uploads">
<system.web>
<compilation debug = "false"/>
</system.web>
</location>
Ø Add Namespace and control to application
i.e.
<system.web>
<namespaces>
<clear />
<add namespace="System" />
<add namespace="System.Collections" />
<add namespace="System.Collections.Generic" />
<add namespace="System.Collections.Specialized" />
<add namespace="System.Configuration" />
<add namespace="System.Text" />
<add namespace="System.Text.RegularExpressions" />
<add namespace="System.Web" />
<add namespace="System.Web.Caching" />
</namespaces>
<controls>
<add src ="~/controls/maleBed.ascx" tagPrefix ="mycontrol" tagName ="male"/>
<add src ="~/controls/femaleBed.ascx" tagPrefix ="mycontrol" tagName ="female"/>
</controls>
</pages>
</system.web>
Ø Session State and View State Settings
<Pages EnableViewState="false" />
<sessionState mode="InProc" />
<sessionState mode="StateServer“ stateConnectionString= “tcpip=Yourservername:42424" />
<sessionState mode="SQLServer" sqlConnectionString="cnn" />
Ø HttpHandler Settings
<httpHandlers>
<add verb="*" path="*.jpg" type="ImageHandler/>
<add verb="*" path="*.gif" type="ImageHandler/>
</httpHandlers>
Ø HttpModule Settings
<httpModules>
<add type ="TwCustomHandler.ImageHandler" name ="TwCustomHandler"/>
<remove name ="TwCustomHandler"/>
<clear />
</httpModules>
Ø Authentication, Authorization, Membership Provider, Role Provider and Profile Provider Settings
Authentication Settings
<authentication mode="Forms">
<forms cookieless="UseCookies" defaultUrl="HomePage.aspx"
loginUrl="UnAuthorized.aspx" protection="All" timeout="30">
</forms>
</authentication>
Authorization Settings
<authorization
<allow roles ="Admin"/>
<deny users ="*"/>
</authorization>
Membership Provider Settings
<membership defaultProvider="Demo_MemberShipProvider">
<providers>
<add name="Demo_MemberShipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="cnn"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
applicationName="/"
requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="5"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression="">
</providers>
</membership>
Role Provider Settings
<roleManager enabled="true" cacheRolesInCookie="true"
cookieName="TBHROLES" defaultProvider="Demo_RoleProvider">
<providers>
<add connectionStringName="dld_connectionstring"
applicationName="/" name="Demo_RoleProvider"
type="System.Web.Security.SqlRoleProvider, System.Web,
Version=2.0.0.0, Culture=neutral, ublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</roleManager>
Ø Config Sections : ConfigSections helps you to create your own custom configuration section that can be used with the web.config file.
<configSections>
<sectionGroup name="pageAppearanceGroup">
<section
name="pageAppearance"
type="PageAppearanceSection"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
</configSections>
Ø Reading configuration section values
// Intialize System.Configuration object.
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
//Get the required section of the web.config file by using configuration object.
CompilationSection compilation = (CompilationSection)config.GetSection("system.web/compilation");
//Access the properties of the web.config
Response.Write("Debug:"+compilation.Debug+"<br/>""+"Language:"+compilation.DefaultLanguage);
Ø Update the configuration section values
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
//Get the required section of the web.config file by using configuration object.
CompilationSection compilation =
(CompilationSection)config.GetSection("system.web/compilation");
//Update the new values.
compilation.Debug = true;
//save the changes by using Save() method of configuration object.
if (!compilation.SectionInformation.IsLocked)
{
config.Save();
Response.Write("New Compilation Debug"+compilation.Debug);
}
else
{
Response.Write("Could not save configuration.");
}
No comments:
Post a Comment