Common ASP.Net Tips and Tricks

This article provides most common tips for errors in ASP.NET as well as configuration. It will be useful for developers who face exceptions while developing web applications.


This article is useful for developers encountering exceptions during development. It also provides some tips for common errors and gives a solution for common exceptions and issues encountered during development.

Tips and Tricks

1. The 500 Error occurs when publishing a website in IIS. The most configuration errors occur due to this.


Configuration Steps related to publish website in IIS
Figure 1: Configuration procedure related to publishing website in IIS

    Step 1: Choose the appropriate application pool and if the target framework is missing in the application pool, use the following procedure depending on your Windows system.

    • Open a command prompt using Run as administrator.

    • Change the directory path for the proper framework in the command prompt.

      CD C:\Windows\Microsoft.NET\Framework64\v4.0.30319

    • After changing the directory path hit Enter "aspnet_regiis i", this command registers aspnet_regiis availability into the directory.

    Step 2: Select proper publish code.

    Step 3: Check that the physical path is accessible for the current user or IUSR by verifying using “Test Settings”.

2. If you have published your website to a live server and want to change some minor code behind file and upload it to live server, then what is the best way?


    Instead of publishing the complete site, use the following:

    Step 1: Just build the project without error and go to the project bin folder, then find "projectname.dll" and "projectname.pdb" (if available).

    Step 2: Then put those two file into the live project's bin folder and just replace it. Now the website runs with the latest changes because our .cs file (code behind file) is converted to a DLL and our page searches for the method reference from the DLL file.

3. Now to resolve the "Maximum request length exceeded" exception when working with a web service or JSON data.


    The 4MB default is set in machine.config, but you can override it in you web.config. For instance, to expand the upload limit to 20MB, add the following code to the web.config:

    1. <system.web>  
    2.    <httpRuntime executionTimeout="240" maxRequestLength="20480" />  
    3. </system.web>  
    IIS7 (and later versions) has a built-in request scanning that imposes an upload file cap that defaults to 30MB. To increase it, you also need to add the following lines:
    1. <system.webServer>  
    2.     <security>  
    3.         <requestFiltering>  
    4.             <requestLimits maxAllowedContentLength="3000000000" />  
    5.         </requestFiltering>  
    6.     </security>  
    7. </system.webServer>  


4. The request filtering module is configured to deny a request that exceeds the request content length (IIS 7).


    You need to add the following configuration to the web.config file.

    1. <system.webServer>  
    2.     <security>  
    3.         <requestFiltering>  
    4.             <requestLimits maxAllowedContentLength="100000000" />  
    5.         </requestFiltering>  
    6.     </security>  
    7. </system.webServer>  


5. The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map (for JSON file).


    You need to add the following configuration to the web.config file.

    1. <system.webServer>  
    2.     <staticContent>  
    3.         <mimeMap fileExtension=".json" mimeType="application/json" />  
    4.     </staticContent>  
    5. </system.webServer>  


6. When I integrated WebAPI2 into an existing MVC web application then some dependency error occurred.


    I reinstalled the "system.web.http.webhost" package using Nuget and added a WebApiConfig file to the ”appstart” folder. Now register to the global.asax file before RouteConfig. If RouteConfig was added before webapiconfig, then the WebApi stops working properly.


    1. public static void Register(HttpConfiguration config)   
    2. {  
    3.     // Web API routes  
    4.     config.MapHttpAttributeRoutes();  
    6.     config.Routes.MapHttpRoute(  
    7.     name: "DefaultApi",  
    8.     routeTemplate: "api/{controller}/{id}",  
    9.     defaults: new   
    10.     {  
    11.         id = RouteParameter.Optional  
    12.     });  
    13.     //To return json format data  
    14.     config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));  
    15. }  
    In global.asax file
    1. AreaRegistration.RegisterAllAreas();  
    2. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
    3. //added before RouteConfig  
    4. GlobalConfiguration.Configure(WebApiConfig.Register);  
    5. RouteConfig.RegisterRoutes(RouteTable.Routes);  
    6. BundleConfig.RegisterBundles(BundleTable.Bundles);  


7. How to speedup page request in ASP.NET.


    Just add the following configuration to web.config file and it will take care automatically.

    1. <system.webServer>  
    2.     <httpCompression directory="%SystemDrive%\inetpub\  
    3. temp\IIS Temporary Compressed Files">  
    4.         <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>  
    5.         <dynamicTypes>  
    6.             <add mimeType="text/*" enabled="true"/>  
    7.             <add mimeType="message/*" enabled="true"/>  
    8.             <add mimeType="application/javascript" enabled="true"/>  
    9.             <add mimeType="*/*" enabled="false"/>  
    10.         </dynamicTypes>  
    11.         <staticTypes>  
    12.             <add mimeType="text/*" enabled="true"/>  
    13.             <add mimeType="message/*" enabled="true"/>  
    14.             <add mimeType="application/javascript" enabled="true"/>  
    15.             <add mimeType="*/*" enabled="false"/>  
    16.         </staticTypes>  
    17.     </httpCompression>  
    18.     <urlCompression doStaticCompression="true" doDynamicCompression="true"/>  
    19. </system.webServer>  



In this article we learned some basic configuration tips as well as common exceptions and their solution. These exceptions occur frequently when developing web applications. I will update this article with other useful tips.