Archive for the 'Software Design' Category

October
7th 2007
URL Rewriting in ASP

Posted under .net & Code & Software Design

“Rewriting” means intercepting a request from the web, and responding with the document behind a different URL. If this sounds like a redirect, it should; the main difference is that we’ll be doing this internally, without the client’s knowledge. ( The “client” is Internet Explorer, FireFox, Safari, or GoogleBot. ) In other words, www.example.com/page.aspx?search=software becomes exposed to the outside world as www.example.com/page/search/software; when somebody asks for the latter, the request is internally sent to the former.

You might do this after restructuring a web site, to prevent links to moved pages from turning into dead ends. Or, you might do it to improve your standing with search engines, the way most WordPress blogs use a post’s date and title, rather than it’s ID in the database, to create “permalinks.” On Apache servers, this is accomplished with mod_rewrite in the .htaccess file, which isn’t available to IIS users.

Instead, developers running Microsoft’s web server, Internet Information Services, or IIS, have three options for rewriting. Let’s examine these, in reverse order by difficulty and cleverness. Continue Reading »

1 Comment »

September
22nd 2007
Requesting Web URL Status From .net Code

Posted under .net & Code & Software Design

There are any number of reasons to want to programmatically interact with resources on the web. Some of the more well known are browsers, like Internet Explorer, FireFox, and Safari; search engine spiders like GoogleBot; and RSS readers. Less famous are automated testing tools checking for 404 (Page not Found) and other errors. As people who maintain sites delve into IIS, cPanel, .htaccess, or custom authentication code, it becomes important to verify that pages are returning an HTTP status code of 200 (OK) when you expect them to, or the right type of redirect (temporary vs permanent). Testing software aims to identify unknown problems in a larger site that becomes difficult to manage by hand.

While some of these tools are worth the price, they can easily become overkill. Especially considering how easy it is to gather this information yourself. Using the built-in functionality of the Microsoft .net Framework, we can gather a wealth of information about any reachable URL. From this point, testing an entire site involves little more than writing a loop. The code provided below doesn’t check whether the host machine is connected to the internet; if not a suitable error will be returned to the caller. For an example of how to test a machine’s connectivity, see this ASP Emporium article.

The WebStatus object defined below is re-usable, in the sense that once an instance of the class has been created, you can either use the object to cache state info until it falls out of scope, or you can use the object again and again to query new URLs. This has nothing to do with object oriented programming, but reduces pressure on the managed heap, allowing your application to use less memory and force less garbage collections.

Continue Reading »

1 Comment »

August
6th 2007
Displaying Graceful Exception Info

Posted under .net & Code & Logging & Software Design

For better or worse, exceptions are a part of life. Any non-trivial application is going to generate them, at least occasionally, when something exceptional happens. One shouldn’t rely on exceptions for control of flow; return values are much better at this in many ways. And a person should run tests to prevent exceptions, when it’s possible … make sure a number isn’t zero before dividing by it.

When an exception, which is really just a modern term for an error, does rear its ugly head, in addition to handling it gracefully, most applications need to do something with it. This might involve showing a message to the user, logging the fact that a problem happened for later inspection, or any number of things. A new open-source .net expands your options.

ExpressionFormatter is a static class, written in C# and targeting the Microsoft.net framework v2, that turns a managed Exception object into an html stream. In an ASP.NET application this lets you send a detailed, easily readable message directrly to the client (after they’ve been authorized!), but in a middle-tier, server-based application html is a fantastic way to store exception details for later review and drill-down analysis. In a Windows Forms client, you can log the exception for later use, and when it makes sense to, you can display it to the client using either a WebBrowser control or by writing an html file and using Process.Start to launch it.

Continue Reading »

No Comments »