kdmurray.blog

The crossroads of life and tech

Accessing HttpContext objects from other classes

I could swear I wrote about this at some point in the distant past, but I couldn’t find the article this week when I needed it to help troubleshoot an issue with another developer. The issue he was having was how to access objects from the executing web page’s HttpContext object from a class other than the CodeBehind of the executing web-forms page. Essentially he was looking for a way to map a web-path to a physical folder path without needing to hard-code it or know where the application was deployed on the server in question.

If done correctly, an application can reside anywhere in the file system and be deployed to a virtual directory at any depth without causing a problem with URL resolution. In the code-behind of a web-forms page, the code is simple:

string physicalPath = Server.MapPath("~/somefolder/myfile.xml");

However doing this from another page involves just a little bit more work:

Using System.Web;
string physicalPath = HttpContext.Current.Server.MapPath("~/somefilder/myfile.xml");

It’s really quite straightforward when you see it, and I can’t believe that I forget how to do it. This method will also provide you access to lots of other useful objects which makeup the “state” of the application from an HTTP perspective.

Making ASP.NET MVC work on IIS 6.0

For the past couple of weeks I’ve finally gotten around to playing with the (now not so new) ASP.NET MVC Framework. This, of course, is Microsoft’s take on the MVC architecture pattern which gives developers another well supported and well documented alternative to traditional WebForms projects.

The test machine that I’m running is Windows Server 2003 and IIS 6.0. Unlike more contemporary versions of IIS, it doesn’t support MVC out of the box and leads to the rather annoying problem of 404′s on every page.

The cause of this is that IIS 6.0 expects a physical file to be available every time a URL is called. When it can’t find the files that match the nice URL routes that the MVC framework uses by default it panics and throws the 404. Thankfully there’s a quick configuration change you can make to fix this issue: enabling ASP.NET to manage wildcard URLs.

IIS6 ASP.NET ISAPI WildcardsStep-by-step Guide

  1. Open up the IIS manager
  2. Open the properties of the site you want to manage
  3. Click on the Home Directory tab, then the Configuration button
  4. Click on Insert
  5. Browse to the ASP.NET ASAPI dll, by default located at: C:WINDOWSMicrosoft.NETFrameworkv2.0.50727aspnet_isapi.dll
  6. Unckeck the box Verify that file exists
  7. Click the myriad OK buttons and voilà!

Now, since that was so simple you may be wondering what the catch is.  Well the catch is this: the wildcard option has IIS process every request into the server, including static files.  Since ASP.NET doesn’t process the files as efficiently as IIS does natively it will cause the application to take a performance hit.  If your applications require really high performance, or have a large user base it might be worth considering upgrading to a newer version of Windows and IIS.

This technique was shamelessly paraphrased from the article on Steve Sanderson’s blog which goes into more detail on the hows and whys behind this structure in IIS.  Steve also offers a few other options which have their own pros and cons.  If you’re making these changes to a production environment I’d recommend you give his article a thorough read.

Enjoy.

ASP.NET MVC Tutorials

A couple of weeks ago at Mix ’09 the ASP.NET MVC team announced the RTW (release-to-web) version of the MVC framework. I’ve been looking at the framework and playing with pieces of it for a few months now, but due to school & work commitments haven’t really had a chance to give it a good run through, or build anything meaningful with it.

This past week I’ve gone back to the ASP.NET website and discovered that there is now a long list of tutorials which have been put in an order to help make the major features of the MVC framework more learnable, particularly for those of us who haven’t had that MVC-heavy comp-sci education.  The tutorials come in either written or video form (there is some overlap) and do provide some good step-by-step instructions for exploring the new methodology.

Expect me to get into more detail about the ins-and-outs of the MVC framework in upcoming editions of the new podcast (more details soon, I promise!!)

You can, of course, download and use the MVC framework with Visual Studio 2009 without the tutorials, but I would highly recommend giving the first few a once-over.  Have a look at the tutorial site and see what you think.