URL Rewriting in ASP.Net Without Extensions

Thu, 13 Dec 2007 09:14:02 GMT

Ever since I started looking into URL rewriting, people have had problems with getting it to work without extensions.  Some solutions require setting IIS up differently or creating additional HTTP Handlers.  The solution I propose isn't a "true" extensionless rewrite, but a bit of a hack that will give the impression of extensionless URLs.

Basically, you need to start with a URL rewrite engine.  You can easily build your own using Application events and built-in Http methods in C#, but I used Microsoft's URL rewriter MSDN example.

URL Rewriting in ASP.NET

Get that example setup and working for your site first.  You may also want to create a Form.browser file in the App_Browsers folder to fix the postback problem that occurs.  You can read more about that here on Jesse Ezell's blog.

OK, so you're up and running with the URL rewrite engine, but your links are still all pointing to something.aspx.  The problem is that without the .aspx extension, the aspnet_isapi.dll filter won't know that it should be handling the request.

The fix basically uses the default document feature in IIS to convince the ASP.Net ISAPI filter to handle the request.  Any folder that has a default.aspx page in it can be referenced simply by the folder url and IIS will pick up on the default document (default.aspx) and process the request.

So, the idea is that you are creating folders on the fly as links are created and putting a blank default.aspx file in the new folder.  The URL for that new folder doesn't have to include the default.aspx page at all.  Instead of a link like http://www.dojotech.com/Blogs/Sean/default.aspx, you simply point to http://www.dojotech.com/Blogs/Sean and IIS handles it the same way.

The bogus default.aspx doesn't event have to be a real page, it can be completely blank.  I use a little function as new links are created called MakeSlugFile.  This does the job of creating the new folder and creating a default.aspx placeholder.

    public static string MakeSlugFile(object slug)
    {
        string sskug = slug.ToString();       
        string slugPath = HttpContext.Current.Server.MapPath(sskug);       
        if (!Directory.Exists(slugPath))
        {
            Directory.CreateDirectory(slugPath);
            File.Create(slugPath + "\\Default.aspx");
        }
        else
        {
            if (!File.Exists(slugPath + "\\Default.aspx"))
                File.Create(slugPath + "\\Default.aspx");
        }

        return sskug;       
    }

Like I said, it's a hack, but it works fairly easily and you don't need to muck about with IIS.

 

  • Comments (1)
  • Pingbacks (0)


Ewan Vs Ewan Episode 1

Mon, 12 Nov 2007 19:14:59 GMT

Ewan Vs Ewan 1

By Jade Kornish

Ewan Vs Ewan: Episode 1

  • Comments (4)
  • Pingbacks (0)


Three Laws of Cowmen

Mon, 05 Nov 2007 09:09:03 GMT

Over the weekend, Rachael got into a bit of genetics research.  Among other things, we learned about current work to create human-enhanced bovine organs for transplantation studies.  Basically the scientists at work on this are growing cow tissue that has been modified to allow it to be implanted into humans without rejecting it.

Even though the goal is to create organs for use in human transplants, the cow-humans are being grown as fetuses (fetii?).  Probably the most interesting aspect of this mad scientist-esque idea is that they actually thought about what would happen if the cow-men grew up.  In an effort to keep the new human-bovine species down, they have engineered it in a way that will keep them from having man-cow babies.  Even if a man-bull and woman-cow happen to grow up to sexual maturitiy and escape their sci-fi grow tanks, their children will be regular humans.  What a shame on the family!

It seems that these scientists are taking a page out of Asimov's book and building in the Rules of Robotics for cow-people.  This is the only rule we found, so I've improvised a bit and come up with the "Three Laws of Cowmen".

  1. A cowman may not injure a "real" human being or, through breeding, allow a cow-human to come to be born.
  2. A cowman must obey orders given to it by "real" human beings except where such orders would conflict with the First Law or if the order is for milk.
  3. A cowman must protect its own existence as long as such protection does not conflict with the First or Second Law or require the cowman to expunge his four stomachs (really, who wants to see four orders of fish and chips on the sidewalk?).

For more reading, check this article on the BBC:

news.bbc.co.uk/1/hi/health/6121280.stm

  • Comments (0)
  • Pingbacks (0)