LANGUAGES: All .NET Languages
ASP.NET VERSIONS: 1.0 | 1.1
ASP.NET Tip-O-Rama, Part 2
Because you can never have too many troubleshooting tips and tricks.
By Don Kiely
In the last issue of my column in asp.netNOW (http://www.aspnetpro.com/art.asp?id=314) I provided tips based on questions I frequently see on some of the ASP.NET forums I monitor. But that, my friends, was just the beginning. Here are a bunch more.
Session Objects Away from Pages: You can access the Session object from a class that doesn't inherit from the Page class this way:
Web forms have this built into them already, which is why you don't need to add this code in a regular code behind module.
Add a Windows Login to MSDE (or SQL Server): Here's how to add the existing ASPNET Windows user to allow access to the db (change the db name, etc., of course):
osql -E -S MachineName\NetSDK -Q "sp_grantlogin 'MachineName\ASPNET'"
osql -E -S MachineName\NetSDK -d NameOfDb -Q "sp_grantdbaccess 'MachineName\ASPNET'"
osql -E -S MachineName\NetSDK -d NameOfDb -Q "sp_addrolemember 'db_owner', 'MachineName\ASPNET'"
Administering MSDE Without Enterprise Manager: MSDE is a great database for some Web applications, but it doesn't come with GUI tools to manage it, and osql from the command line can be tedious. ASP.NET Enterprise Manager is an open source SQL Server and MSDE management tool. Microsoft's ASP.NET Web Matrix includes a database management tool. Microsoft's Web Data Administrator is a free Web-based MSDE management program written using C# and ASP.NET, and includes source code. You can also access MSDE using Access.
No Stinkin' Back Button! If your app just can't tolerate a user clicking the back button, you can tell the browser to not cache the page. Most of the time it will listen if you use this code, which covers all the bases:
Response.Expires = 60
Response.ExpiresAbsolute = DateAdd(DateInterval.Day, -1, Now())
Response.CacheControl = "no-cache"
You can do this all programmatically as well, to customize the behavior of the page, such as to show a message that something has expired.
Cache Cleaning Day: Sometimes VS .NET's VSWebCache directory gets out of sync with reality. You can clean it out when things just don't seem be working right in VS .NET. Close VS .NET, go to C:\Documents and Settings\[userlogin]\VSWebCache\[machineName], and delete the subdirectory that corresponds to the project. When you start VS .NET again, it will recreate it. You should also clean out the directory at C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files (this path may be a bit different depending on your Windows directory name and the version of the framework you're running).
ASP.NET Recycling: Recycling is usually good and will save the planet, but ASP.NET recycling can be bad. Sometimes you get this message in your event logs:
"aspnet_wp.exe ... was recycled because it was suspected to be in a deadlocked state"
ASP.NET has a feature that, if it suspects that a process is deadlocked, restarts itself. This makes the service a lot more robust, but it can also be triggered by processes that take a long time to generate the requested page. You have two choices, in most cases. One, and this is usually the best one, is to simplify the page processing so that it doesn't take so long. This sometimes isn't feasible for all situations, such as long, complicated database queries, but there are ways around it. The other option is to change the executionTimeout setting in the <httpRuntime> element in machine.config. It's probably set to 180 seconds, although Framework updates seem to change that to 90 seconds. You can increase that value, but keep in mind that doing so will increase it for all ASP.NET apps on that machine. If an ASP.NET app really does freeze or die, it'll take that much longer for ASP.NET to restart itself.
If you have any tips or some refinements of these, please let me know. Or, better yet, come visit the Microsoft-sponsored ASP.NET community at http://www.asp.net and share them there!
Don Kiely is senior technology consultant for Information Insights, a business and technology consultancy in Fairbanks, Alaska. E-mail him at mailto:[email protected].