Few Best Coding Practices for ASP .NET

Be it any software language, the features that you opt from the language and the way you code reflects the Performance of an Application. Few such features are discussed below.

Use NGEN.EXE to Fasten Application Loading

NGEN.EXE is the Navigator Image Generator Utility. This utility runs the Just-In-Time (JIT) Compiler. The machine code generated by the compiler is cached as Native Images in the disk. During runtime, these images in the cache are used to run the code. This avoids generating the code dynamically and thereby it fastens the application startup/loading time. To install NGEN.EXE, use the following command as part of Application Setup Program:
ngen.exe install <name of the assembly> / queue:<priority>

When you execute the above command, assemblies that have high impact on application loading/start up are precompiled and stored in native cache of the disk.

The command to uninstall this utility is as follows:
ngen.exe uninstall <name of the assembly>

Ensure Disabling of Debug During Deployment of the Application

When you create a new application, Debug mode is enabled by default. This is helpful during Development phase. But during Deployment of an application in production environment, it highly affects the performance of your application. The reason mainly because the caching is not done when debug mode is enabled. So the scripts, libraries and static images have to be downloaded on each request. Hence always ensure that Debug mode is disabled before deployment. You can easily do this by setting the attribute “debug” as false inside the <compilation> section of Web.config file. Given below is a fragment of Web.config file which has debug mode disabled:

<configuration>
<system.web>
<compilation defaultLanguage = “VB” debug=”False”>

</compilation>
</system.web>
</configuration>

Usage of ’&’ Versus StringBuilder for String Concatenation

Usage of ‘&’ or StringBuilder depends on the number of concatenations that has to be done. If you are going to do less than five concatenations, then its better you use ‘&’. Incase if it’s going to be five or more concatenations, then usage of StringBuilder is recommended. The reason because String is immutable and StringBuilder is mutable. Given below are the sample code fragments so that you can understand things much better:


Dim resultString As String = “Best Coding Practices ” & “for ” & “ASP.NET”
Response.Write(resultString)

Dim strBldr As New StringBuilder(“The Fruits which I like are ”)
strBldr.Append(“Apple,”)
strBldr.Append(“Banana,”)
strBldr.Append(“Orange,”)
strBldr.Append(“Strawberry”)
Response.Write(strBldr.toString());

Use Caching to Avoid Reloading of Pages

In a web application, you might need to access certain pages more frequently. Instead of reloading those pages every time, the page content can be cached. This improves performance and increases throughput. For this cache feature got to be enabled, and ASP.NET has a directive termed @OUTPUTCACHE

<%@ OutputCache Duration = “25” VaryByParam = “UserName” %>

Duration = “25” means that the page is cached for 25 seconds. If the request for reloading the page is placed within 25 seconds then the content will be fetched from the cache. If the request is after 25 seconds then the page will be regenerated dynamically.

VaryByParam can have one of the following values:

VaryByParam=<Specific Parameter name>: Assuming that the page request contains two parameters username and securitykey, the url might look like:
http://localhost/testApp/LoginForm.aspx?username=testuser&securitykey=1234

Here the parameters are username and securitykey. If VaryByParam=”username” then the content will be fetched from the cache until the page request is done for the same username. To make things much clearer for you…

After the initial request made as shown above, the content will be cached. Now if the next request comes with the parameters as shown below:

http://localhost/testApp/LoginForm.aspx?username=testuser&securitykey=1234

The page content will be displayed from the cache because the username is “testuser” in both the cases If the request is from a different user (as shown below), then the page will be regenerated dynamically.

http://localhost/testApp/LoginForm.aspx?username=adminuser&securitykey=1234

Values of more than one parameter can also be checked in one go. For example,

VaryByParam=”username;securitykey’

VaryByParam=”None”: If no parameter has to be checked against

VaryByParam=”*”: Check against all parameters in the request. Instead of specifying VaryByParam=”username;securitykey”, we can simply specify it as VaryByParam=”*”

| Additional Ways of Ensuring Security in .NET | Memory Lifecycle in .NET – An Overview | Few Best Coding Practices for ASP .NET | Handling Session Efficiently Using SQLSERVER State Management in .NET | How to Restrict a Program to Single Instance in .NET? | How to Use Structured Exception Handling in .NET | Understanding Boxing Versus Unboxing in .NET | Understanding Different Levels of Security in .NET | Understanding the Disadvantages of Memory Management in .NET | Using Membership API for Secured Coding in .NET |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.