Saturday, January 7, 2012

Configuring an ASP.NET 4.0 Web Application

We often wonder why configuring ASP.NET web applications sometimes is a pain.  It's actually not, we just forget to do a little tweaks which is why I am writing this post:  so that I don't forget next time!

Ok. So here's the situation, I had to reinstall my Windows 7 Ultimate on my 64-bit machine.  All my web application code was in that box so that meant i had to configure my development environment from scratch!  So I Installed SQL Server 2008 R2 before Visual Studio 2010 Ultimate.  Yes, that is correct, SQL Server first. I've had experiences when installing VS first would install its own SQL Server Express edition, then installing SQL Server 2008 R2 would use that SQL Server Express instance.  I could have missed something during the installation but I always find that installing SQL Server before VS is nuch smoother.

So now i installed IIS 7.5 by going to Control Panel -> Programs -> Turn Windows Feature On or Off and check Internet Information Services.  Remember to check the sub features under these if you need any of them.  I had to check ASP.NET (of course!) under Application Development Features and URL Authorization under Security.

Then I add a Web Application in IIS.  You could add a Web Site with a different port number (such as 8080, 8081, etc) or you could just add  a Web Application under the Default Web Site which is port 80 by default.  Then you add a new Application Pool because the default application pool uses .NET 2.0.  So make sure your new Application Pool (which you can call anything you want) must be using .NET 4.0.  Also make sure that you go to Advanced Settings and select the correct Application Pool Identity.  In my case, I chose Network Service because that's the same account that is accessing my SQL Server database.

After all this, you might find that your pages still won't run.  Here are some of the errors that you might see:
  
HTTP Error 500.19 - Internal Server Error with Error Code: 0x80070021 
The requested page cannot be accessed because the related configuration data for the page is invalid.
Config Error: This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false". 

 To get around this error, you will have to open this file

 Windows\System32\inetsrv\config\applicationHost.config

Change both these entries from Deny to Allow:

<section name="handlers" overrideModeDefault="Allow" />
<section name="modules" allowDefinition="MachineToApplication" overrideModeDefault="Allow" />

HTTP Error 404.3 - Not Found

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.
 

HTTP Error 500.21 - Internal Server Error
Handler "PageHandlerFactory-Integrated" has a bad module "ManagedPipelineHandler" in its module list

These errors appear because possibly you have not installed ASP.NET and registered it with IIS.  So you should follow these steps.

If you have not installed the ASP.NET feature, add it through the Control Panel -> Programs -> Turn Windows Feature On or Off and check Internet Information Services I mentioned above.

Then register ASP.NET with IIS by doing the following.

Go to c:\Windows\Microsoft.NET\Framework\v4.0.30319 and run aspnet_regiis.exe -i

If you have a 64-bit machine, go to the c:\Windows\Microsoft.NET\Framework64\v4.0.30319 folder instead.

An error has occurred: 0x8007b799
You must have administrative rights on this machine in order to run this tool.
 


Turning off the UAC fixes this issue.

Start > Control Panel > System and Security > Action Center > Choose Your UAC Level - Set this to Never Notify (Requires a restart);

After doing all these little tweaks, my web application was running fine!

Good luck!