Rick van den Bosch - Blog

... on .NET, software architecture, software development and whatnot

Recent Posts

Tags

News

  • Live space

    Photo blog

    Follow me at twitter

    Rick  van den Bosch

    LinkedIn profile

    Add to Technorati Favorites

    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Community

Email Notifications

Blogs I read

Interesting links

Archives

October 2005 - Posts

IBM certified: Rational Unified Process
I'm having a good weekendstart today: I took an exam for Rational Unified Process this afternoon, and passed with a score of 91%! I am now an IBM certified Rational Unified Process professional. Sweet.
Howto: create a custom httpHandler

If you would like to (for example) make sure all the jpegs on your site are displayed including a copyright text or something like that, you could use the example I posted before. But it would be easier to write a custom httpHandler. Here's a small howto:

1. Add a class to a WebApplication, and give it a decent name. Mine is customHandler.
2. Have the class implement the IHttpHandler interface (found in the System.Web namespace)
3. Implement the right methods
4. Use 'ProcessRequest' to process the request the way you want to
5. Register the handler in the web.config
6. 'Register' the handler in IIS
7. You're good to go!

Now for some exampe-code. Steps 1 to 4 can be found here:

public
class customHandler : IHttpHandler
{
    public customHandler()
    {
    }

    public void ProcessRequest(HttpContext context)
    {
        Image image;
        string plaatje;
        Graphics graphics;

        plaatje = context.Request.PhysicalPath;
        image = Image.FromFile(plaatje);
        graphics = Graphics.FromImage(image);

        //TODO: add dynamic calculation of the drawstring location
        graphics.DrawString("bloggingabout.net",
new Font("Verdana", 14, FontStyle.Bold), new SolidBrush(Color.White), 10, 280);
        graphics.Dispose();

        context.Response.ContentType = "image/jpeg";
        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        context.Response.End();
   
}

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
}

Step 5 looks a little bit like this:

<httpHandlers>
    <add verb="*" path="*.jpg" type="handlerTest.customHandler, handlerTest"/>
</httpHandlers>

Step 6 is a little bit tricky. You will have to open the Internet Information Services manager and open the properties for the the website you want the handler to work for. Om the first tab page, open up configuration. Now, first select a file extension which is served by ASP.Net, like .asax or .aspx and click edit. Copy the file that's used over there (it should be C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll). Close the dialog and click to add a new one. Here, put the copied value in the right textbox and fill in .jpg as the extension you want to be handled. Next close all open boxes by clicking OK and you should be ready to go.

The image it produced with my code:

Internet Explorer Developer Toolbar (Beta)

Today I stumbled upon the Dutch Microsoft blogs and found an interesting post at the blog of Robert Fransen: the IE Developer Toolbar beta is available! The Toolbar offers you lots of handy features:

  • Explore and modify the document object model (DOM) of a web page.
  • Locate and select specific elements on a web page through a variety of techniques.
  • Selectively disable Internet Explorer settings.
  • View HTML object class names, ID's, and details such as link paths, tab index values, and access keys.
  • Outline tables, table cells, images, or selected tags.
  • Validate HTML, CSS, WAI, and RSS web feed links.
  • Display image dimensions, file sizes, path information, and alternate (ALT) text.
  • Immediately resize the browser window to 800x600 or a custom size.
  • Selectively clear the browser cache and saved cookies. Choose from all objects or those associated with a given domain.
  • Choose direct links to W3C specification references, the Internet Explorer team weblog (blog), and other resources.
  • Display a fully featured design ruler to help accurately align objects on your pages.

Find the original (Dutch) post of Robert Fransen here, the IEBlog post here and the download here.

HowTo: implement a list that sorts based on value ( not key ! )
Sometimes you need a sorted list. You would think in that case the SortedList class would help out, but unfortunately this one only sorts based on the key, while most of the time you would like to sort based on the value. 

Now I can almost hear you think: "Why not switch key and value then?" Well, let’s say the key you would like to use is the username for a software program or a website. The value is the full name of the user. It is possible two different users have the same full name (John Smith), but have different usernames (j.smith and johns). In that case, switching keys and values won’t help you, because keys need to be unique in a SortedList.

To implement your own ‘sorted list’ which sorts based on the value, first create a class (or a struct) to hold the data for the key and the value you would like to retain in the list. Make sure you implement the IComparable interface, because you will need this to be able to sort the class/struct. For this example I implemented both key and value as a string:

public struct KeyValuePair : IComparable
{
    private string theKey;
    private string theValue;
           
    public string Key
    {
        get
        {
            return theKey;
        }
    }
 
    public string Value
    {
        get
        {
            return theValue;
        }
    }
 
    public KeyValuePair(string key, string value)
    {
        theKey = key;
        theValue = value;
    }
 
    public int CompareTo(object obj)
    {
        if (obj == null)
        {
            return 1;
        }
        else
        {
            KeyValuePair check = (KeyValuePair)obj;
            return Value.CompareTo(check.Value);
        }
    }
}
 
Now when you want a value-sorted list, you can simply create an ArrayList, add KeyValuePairs to the ArrayList, call the Sort() method (which calls the CompareTo() method for each of the objects that exist in the ArrayList) and see that the items in your ArrayList are sorted based on the values of all KeyValuePairs!!!

Sample:
    ArrayList arrayList;
    arrayList = new ArrayList();
    arrayList.Add(new KeyValuePair("1", "bbb"));
    arrayList.Add(new KeyValuePair("2", "ccc"));
    arrayList.Add(new KeyValuePair("3", "aaa"));
    arrayList.Sort();
    foreach (KeyValuePair keyValuePair in arrayList)
    {
        proofTextBox.Text += keyValuePair.Key + " : " + keyValuePair.Value + Environment.NewLine;
    }