Thursday, December 27, 2012

Hosting images using Handler (ASHX)

In HTML, when we want to display images, we are using the following tag:

   <img src="images/myProfilePhoto.jpg"/>

That's very simple and has nothing much to complaint. But, you won't be able to trace who has requested the image file especially when the privacy issue comes into place... it becomes very nasty. In this case, we need to use ASP.NET Handler (ASHX) to host the images.

Advantage of hosting images through Handler:
  • You can hide the image file path. For example, your web app is located in D drive and all image files are stored in E drive (where you don't have to expose the image folder in IIS).
  • You may track down the visitor information that includes their IP address, browser, etc.
  • You may stored the images in a database and host it through the Handler - you don't need to create an image file physically and then write the phyicsal file name into "SRC" attribute. You just need to set the SRC attribute to your ASHX handler (refers to the sample code in the link below).
  • You may cache the images in the server.

In the HTML, you will have something like this:

  <img src="showPhoto.ashx?memberID=0001"/>

Where "showPhoto.ashx" is the Handler that you develop and "memberID" is the query parameter. Your handler should return the profile photo (in byte array) for member ID 0001.

For the code on how to implement the Handler or ASHX:

  http://stackoverflow.com/questions/994135/image-from-httphandler-wont-cache-in-browser
  http://stackoverflow.com/questions/1507572/streaming-databased-images-using-httphandler

For full explanations on the Handler:

   http://support.microsoft.com/kb/308001

Friday, December 14, 2012

Prevent the setting nested into sub-virtual directories

If you have a virtual directory which contains sub-virtual directories and you don't want the root virtual directory settings (in the web.config) to be inherited into the sub-virtual directories, you have to wrap the "system.web" with "location".

  <location path="." inheritInChildApplications="false">
    <system.web>
    ....
    </system.web>
  </location>

Tuesday, December 4, 2012

Looking for clue which website crash IIS

I have about 10 ASP.NET web applications running on the same IIS server. Somehow, I made changes to  "some" libraries and I could not figure out which web application keep crashing IIS server.

After many days in googling the solution, I found out that there is a freeware program which is able to open .WER file. Within this crash dump file, you will be able to find out which web application crashed your IIS.

   http://www.nirsoft.net/utils/app_crash_view.html

You will be surprise when you run this program because it will show all the crash dump files in your server/laptop.