In this short tutorial i’ll try to show you how to route webrequests to your generic handlers. This topic seems to be a little untouched on msdn, which is weird, because it’s very important to rewrite for example the url of images that you use on your website.

So instead of rendering html like http://yourcoolwebsite.com/imagehandler.ashx?id=10 in your image’s source tag you’ll want to have something like http://yoursweetwebsite.com/awesome.jpg .

.NET Routing allows you to do this very easily (it’s true!).

Here’s what you’ll want to do:

  1. create a class that implements the IHttpHandler interface
  2. create a class that implements the IRouteHandler interface
  3. do the inner plumbing (i’ll get to it don’t worry)
  4. reference your url format in the global.asax
  5. excelsior!

So instead of using an .ashx file we’ll just create an ordinary class:

.net routing to generic handler: the classes

So what we have here is both an httphandler class and the routehandler.

As you can see you can pretty much just copy the processrequest from your old generic handler to the new processrequest method of the httphandler.

The RouteHandler will basicly supply the needed request context to our HttpHandler, this is where we’ll get the parameter from; the id for example of the image to return. In the Generic Handler of old we used to get the value of the querystring from the httpcontext, this is ofcourse useless now because we need the value from our Route context.

Here’s where the publicly defined RequestContext comes in to play. As the request to the routehandler is being made we return our very own HttpHandler but first set the RequestContext. Now you’ll have all your Routing variables available within your HttpHandler.

As for the Global.asax you’ll want to add something like this :

routing to the httphandler in your global asax

As you can see, all requests that comply to this sort of route will be routed to our – very geekalicious – routehandler.

The ImageUrl can now be completed something like this: AvatarImage.ImageUrl = string.Format(“~/avatars/{0}-{1}.jpg”, “somename”, “someuniqueidentifiermaybe”);

So as usual, hope this helps someone! And i’ll be glad to hear about any mistakes or stupid thing i’ve said.

 

Here’s another one of those classic Gridview problems:

I’ve got a Gridview that needs to have a RadioButtonList in it so that users can select only one row.

You can’t do this in ASP.NET with use of the <asp:RadioButton runat=”server”  />. Even though you can fill in a groupname it doesn’t work.
The reason it doesn’t work is that the rendered radiobuttons all have a different “name” attribute (you know, because the namingcontainer requires that all controls have a unique id).

The solution is very easy though; instead of using the .NET radiobutton control we will use a plain old <input type=”radio” />

Just like this:

<input type=”radio” name=”defaultfilegroup” value=’<%# Eval(“resource”) %>’ />

The name attribute works like the groupname; make it equal for all relevant radiobuttons.
The value attribute will contain the value of what the user selected. This is what we’ll retrieve as the selected option.

Retrieving the selected option:

supposing you save all changes when the user hits the “save” button here’s what we’ll do:

    public void SaveSetupLinkButton_Click(object sender, EventArgs e)
    {
        string selectedFile = Request["defaultfilegroup"];
    }

Whoa! That’s how easy it is.

Like this? Use the share buttons below!

© 2012 Dries Hoebeke on .NET Suffusion theme by Sayontan Sinha