Apache Style
One of the things I like best about ASP.NET is how Apache's httpd.conf files were mimicked by web.config files. Cuts down a bit on restarting the service (don't think we'll ever get to the point where reboots are minimal though) when certain changes to your web application(s) are made. So I figured it would be nice to keep a bunch of important settings in one place and update this page as needed.
Shared Code Folders; C# and VB Classes in ~/App_Code
2009-09-15
Please don't ask me why anyone would want to do this - if I had a choice I would never EVER write a line of VB.NET code. I could go on and on (and on) about why - case-insensitive, stupid underscore statement continuation, array declarations, multi-line strings...oh and I said I wouldn't, better stop now...
Anyway I don't have a choice at work. Oh the joy! Fortunately I've found one thing (minor miracle) that VB has over C#, and maybe I'll get around to writing about it one of these days. In the meantime, add the codeSubDirectories element to web.config:
<configuration>
<system.web>
<compilation>
<codeSubDirectories>
<add directoryName="VBCode" />
<add directoryName="CSCode" />
</codeSubDirectories>
</compilation>
</system.web>
</configuration>
Now you can dump classes in either language in the respective ~/App_Code subdirectory; obviously you can name the directoryName attribute value anything you want.
References
ASP.NET 2.0 Error Handling
2007-04-04
The traditional way to log unhandled exceptions is described here; add custom code in the Application_Error event handler of global.aspx. Using ASP.NET 2.0 Health Monitoring you don't need to write any code to get very detailed error information.
For example, let's say that you want to have all unhandled exceptions emailed to your web developers. This is all you need to do:
<?xml version='1.0' encoding='utf-8' ?>
<configuration>
<system.web>
<!--
this is needed whether you're using Application_Error
or Health Monitoring
-->
<customErrors defaultRedirect='~/YOUR_ERROR_PAGE.aspx'>
<!--
this section onward specific to Health Monitoring
-->
<healthMonitoring enabled='true'>
<providers>
<add
name='YOUR_UNIQUE_PROVIDER_NAME'
type='System.Web.Management.SimpleMailWebEventProvider'
to='WEB_DEVELOPERS_EMAIL_ADDRESS'
from='FROM_EMAIL_ADDRESS'
buffer='false'
/>
</providers>
<rules>
<add
provider='YOUR_UNIQUE_PROVIDER_NAME'
name='UNIQUE_RULE_NAME'
eventName='All Errors'
minInstances='1'
maxLimit='Infinite'
minInterval='00:01:00'
/>
</rules>
</healthMonitoring>
</system.web>
<!--
this section is needed to send the email
-->
<system.net>
<mailSettings>
<smtp deliveryMethod='PickupDirectoryFromIis' />
</mailSettings>
</system.net>
</configuration>
Notes
- ASP.NET 2.0 includes a default
web.config file that records web events to the event log. It is located in the %SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727\CONFIG directory.
- Tune the
minInterval attribute of the add Element for rules to whatever time interval you feel comfortable with. If you set the threshold too low, you potentially risk filling your developers' mailbox with pretty much duplicate emails.
- If you didn't guess, the
eventName attribute of the add Element for rules is the setting that specifies which web events are monitored. All Errors is a "friendly mapping" configured in the machine's default web.config file.
- The system.net section above assumes that the SMTP service is running on the web server.
- ASP.NET 2.0 Health Monitoring offers many different providers to handle web events. We've chosen email notification by setting the
type attribute of the add child node of the providers node, which is perfect for sites like this that are hosted in a shared environment where you don't have access to the event log.
References
User/Server Controls
2007-03-27
Have a common set of user controls or custom server controls deployed on your web site? User controls are typically used declaratively in an .aspx page:
<%@ register tagprefix="MY_TAG_PREFIX" tagname="MY_TAG" src="USER_CONTROL.ascx" %>
// your user control
<MY_TAG_PREFIX:MY_TAG runat='server' />
As are server controls:
<%@ register tagprefix="MY_TAG_PREFIX"
namespace="MY_NAMESPACE" assembly="MY_ASSEMBLY"
%>
// your custom server control
<MY_TAG_PREFIX:MY_CLASS_NAME runat='server' />
You can make life a little simpler for yourself by registering the controls in web.config:
<?xml version='1.0' encoding='utf-8' ?>
<configuration>
<system.web>
<pages>
<controls>
<!-- user control -->
<add tagPrefix='MY_TAG_PREFIX' tagName='MY_TAG'
src='~/controls/MY_USER_CONTROL.ascx'
/>
<!--
server control; 'assembly' attribute *NOT* required
if the control lives in ~/app_code
-->
<add tagPrefix='MY_TAG_PREFIX'
namespace='MY_NAMESPACE' assembly='MY_ASSEMBLY'
/>
</controls>
</pages>
</system.web>
</configuration>
Controls declared in
web.config do NOT need to be declared in individual pages using the
Register directive; see
controls Element for pages.
File Uploads
2007-02-23
Although not mentioned in
my file upload example ASP.NET 2.0 give you a
huge improvement over 1.XX; more control and much less overhead. In 1.XX file uploads, regardless of size, were sucked
in their entirety to memory. Thankfully M$ fixed this idiotic model in 2.0, reading uploaded files in chunks and storing them in a temporary directory. The specific settings with their default values:
<?xml version='1.0' encoding='utf-8' ?>
<configuration>
<system.web>
<!-- executionTimeout; in seconds, tune to expected file upload size -->
<!-- maxRequestLength; max allowed file upload size in **bytes** -->
<!-- requestLengthDiskThreshold; buffer/chunk read size in **bytes** -->
<httpRuntime
executionTimeout='110'
maxRequestLength='4096'
requestLengthDiskThreshold='256'
/>
</system.web>
</configuration>
httpRuntime Element SDK documentation.