Tuesday, 17 June 2014

ASP.NET Web.config file



ASP.NET Web.config file...
We should have good knowledge about the Web.config file, because it's a major character when we going to develop web applications using ASP.net. First, I like to give some examples "What we can do using the Web.config file"...And after I'll discuss how we going to code the examples.
  • Can create database connection using the Web.config file
  • According to the user log in system environment, we can modify the Authentication and Authorization.
  • We can set image path for the uploading images.
  • Also we can manage the lager size files, when uploading to the web server.
  • If the System.Net.Mail email sending, we should configure the Web.config file.
How to Create Database connection in the Web.config file
You can write following code before the <system.web> tag and inside the <Configuration> tag
<connectionStrings>
 <add name="NameConnectionString" connectionString="Data Source= (Default);Initial Catalog=DatabaseName;User ID=Username;Password="Your Password""providerName="System.Data.SqlClient"/>
</connectionStrings>
How to manage Form authentication and the authorization in the Web.config file.
Following code demonstrate the way it can be implement.
<authentication mode="Forms">
<forms loginUrl="login page url" timeout="30"></forms>
</authentication>

Then you should show the location which need to be restricted from anonymous users.
After the <system.web/> tag we can write following code to above purpose. It's pretty cool, isn't it..?

<location path="FolderNameAuthenticationNeed" allowOverride="true">
<system.web>
<authorization>
<deny users="?"/>
</authorization> </system.web>
</location>

How we should configure the Web,config file according to the E-mail sending...
After the <system.web/> we should change following changes to the Web.config file

<system.net>
<mailSettings>
<smtp>
<network
host="Host name"
port="25"
userName="Username"
password="password" />
</smtp>
</mailSettings>
</system.net>

How to set the Image path when uploading images to the web..

<appSettings> <add key="ItemImagePath" value="Specify the path to save emages"/>
</appSettings>

How to upload a larger size file into the web server...
additionally we have to configure the Web.config file as follows.

<httpRuntime executionTimeout="90" maxRequestLength="4096"useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4"appRequestQueueLimit="100"/>

You can use the httpRuntime section to configure a number of general runtime settings, two of which are particularly convenient.
1.             <httpRuntime appRequestQueueLimit="100" executionTimeout="600" />  
The first attribute specifies the number of requests the server may queue in memory at heavy-traffic times. In the example, if there are already 100 requests waiting to be processed, the next request will result in a 503 error ("Server too busy").
The executionTimeout attribute indicates the number of seconds for which ASP.NET may process a request before it’s timed out.


<customErrors>

To provide your end users with custom, user-friendly error messages, you can set the mode attribute of this section to On. If you set it to RemoteOnly, custom errors will be shown only to remote clients, while local host users will see the ugly but useful ASP.NET errors -- clearly, this is helpful when debugging. Setting the mode attribute to Off will show ASP.NET errors to all users.

If you supply a relative (for instance, /error404.html) or absolute address (http://yourdomain.com/error404.html) in the defaultRedirect attribute, the application will be automatically redirected to this address in case of an error. Note that the relative address is relative to the location of the Web.config file, not the page in which the error takes place. In addition you can use <error> tags to provide a statusCode and a redirect attribute:

<customErrors mode="RemoteOnly" defaultRedirect="/error.html">
<error statusCode="403" redirect="/accessdenied.html" />
<error statusCode="404" redirect="/pagenotfound.html" />
</customErrors>

No comments:

Post a Comment