ASP.NET Web Application Without .ASPX Extension

The Problem

Ok, let’s say you want to create an application in ASP.NET. That application should be able to execute .NET code regardless of the path that is requested. You don’t want to map the .NET execution to a specific extension and you want handle all requests in a simple manner. Well, I think I figured out how.

In the Java Servlet world, you can easily map arbitrary paths to a servlet handler, such that all request to ‘/somedirectory’ are handled by your ‘SomeDirectoryServlet’. Ruby on Rails uses a controller as well and you can do URL rewriting with that controller very easily to support arbitrary path schemes. But in the .NET/IIS world, all of the configuration for handling is based solely on file extensions.

An Actual Project That Needs This

At SpiderLogic (where I work), we are working on a client project where their current infrastructure is all .NET. Their current application is entirely web based. It allows users to (among other things) upload files and share them among a group of users. Now they want to allow users to access files on a Windows desktop and interact with them in a much more natural way, by dragging and dropping, etc. Windows Explorer has the ability to mount WebDAV paths the same as it can with windows file sharing. So, basically we wanted to see if we could build a virtual WebDAV server that would allow us to read file meta-data from a Database and then expose that over the web as a WebDAV share.

WebDAV is an extension to HTTP that allows for Distributed Authoring and Versioning. Basically it looks a lot like HTTP, where it has verbs and requests files.

A WebDAV header might look something like:

PROPFIND /some/path/
HOST www.example.com

The Solution

As I said above, IIS exclusively uses file extensions to map files to code to execute the file. .NET provides HTTP Handlers and HTTP Modules that allow you to handle requests that should not be handled by a traditional Page. A Module registers itself when it is loaded to respond to specific events in the HTTP Request life-cycle. Sometimes this is called an Intercepting Filter. Modules listen to events and can respond to that event. They can do many things, check security, add content to the response, transform an XML response into an appropriate form based on the calling device, etc. They can also take over the entire request-response and handle it in its entirety.

For our example, we want to respond to a very early event – BeginRequest and fulfill the request ourselves.


using System;
using System.Web;

public class DavModule : IHttpModule {

private HttpApplication context = null;

public void Dispose() {
context.BeginRequest -= new EventHandler(context_BeginRequest);
}

public void Init(HttpApplication context) {
this.context = context;
this.context.BeginRequest += new EventHandler(context_BeginRequest);
}

void context_BeginRequest(object sender, EventArgs e) {
HttpApplication app = (HttpApplication) sender;
app.Response.ContentType = "text/plain";

if (app.Request.Path.EndsWith("Help"))
app.Server.Transfer("Help.aspx");
else {
app.Response.Write("Path: " + app.Request.Path);
app.Response.End();
}
}
}

The IHttpModule responds to begin request events and takes over handling the fulfillment of the HTTP response. The example demonstrates that you can get the request path and then parse it to handle different cases. One use would be for doing internal URL rewriting (although I suspect that this kind of URL rewriting would break the post-back of .aspx files). As I said at the start, we plan on trying to implement a virtual directory using WebDAV. The possibilities are limited only by your imagination.

The handler itself is made available to a web application by registering it in the Web.config file for the application.

Web.config configuration:






Of course if that was all there was too it, this would be a much less interesting article. The trick to making this all work is to properly configure IIS. We want to handle all requests to our application using the .NET code that we have written. We don’t want IIS to return 404s (Not Found) for files that don’t exist since we want to implement a dynamic, virtual directory and not just deal with files on the file system.

To do this, you have to remove all of the existing handlers under the Application Configuration. Then you want to map all extensions (.*) and in our case all Verbs (GET, POST, PROPFIND, MKCOL – which allows us to hopefully respond to WebDAV verbs as well) to the aspnet_isapi.dll.

Overview of ISS configuration.

Here’s the editing screen that you will see:

Details of ISS configuration.

This was Step 1 of the proof of concept to see if we can make the WebDAV system work. Hopefully a future posting will be about the success of the implementation.

This is a very non-obvious thing to do in IIS/ASP.NET, but for some applications it is a necessary thing. Of course if you have more control over the tools and language you use to implement it, then maybe you could choose something that was better suited for this application. Maybe someone else will find this useful? What do you think?

Thanks to Brennan and Oliver who work at SpiderLogic for their help with this.

Update
Under Windows Server 2003: .* is not allowed in the mapping , there is an extra ‘all extensions’ section where you can add the aspnet_isapi.dll.

22 Replies to “ASP.NET Web Application Without .ASPX Extension”

  1. Hi,
    For the past half an hour i was struggling with IIS6 trying to force all incoming requests to be handled
    by asp.net (aspnet_isapi.dll). In IIS5, you had to open IIS manager and from website properties select “Home directory/Configuration/Mapping” add a new extension * and point to : C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll

    According to help topic it should be the same for IIS6, but when i tried to put * or .* it didn’t accept it , and
    all what i was getting is an error “Wrong extension format”. After almost half an hour i finally figured out that it shoud be added in a bit different way: from website properties select “Home directory/Configuration/Mapping” and choose Insert instead of Add, under “Executable” enter path to aspnet_isapi.dll and uncheck “Verify that file exists”, otherwise it wont work for folders. Unfortunately this also doesnt help.

    In Microsoft’s support site (http://support.microsoft.com/kb/326965/en-us) they say to update the existing list of MIME types. I did that also, but unfortunately this also does not solve the problem.

    Please help me out if you have any solution.

    You can reach me at vishvesh888@hotmail.com or vishveshraiter@gmail.com

    Thanks
    Vishvesh Raiter

  2. Any news whether you succeeded in building your virtual WebDAV server? I’m currently planning the same for a customer and would even buy the code if it’s appropriate.

  3. UK,
    Yes, please see my other posts on WebDAV. The code was written for a client so I can’t sell it to you unfortunately. Get in touch if you’re interested in hiring a consultant though. :)

  4. Thank you, Geoff

    I searched your Weblog with the “WebDAV” keyword and only found this article. Any hints which articles you are refering to?

    Cheers from Germany
    Uwe

  5. UK,
    Sorry, it looks like one of my WordPress plugins was breaking search. Try again or browse the WebDAV category found at the bottom of the page.

  6. Congratulations, you have the only page decent on WebDav information on the entire internet (Yes, I have searched them all…)
    Let just cut to the chase – I’m desperate! I really need this to work. I feel I’m close, but I’m stuck.

    I have successfully…
    • Created a HttpModule/HttpHandler which can be accessed by DavExplorer.
    • Once been able to connect to the server using Windows Explorer, but only once…
    • Using Windows Explorer (my only one), I can do directory listing (OPTION & PROPFIND)
    • Through DavExplorer I can receive all verbs from the client
    However…
    • I can’t make new connections with Windows Explorer. I either get “The folder you entered does not seem to be valid”, or I get so far as naming the Web Folder. But when finishing, there are no folder there (-yes, this one is my favorite)
    • Using Windows Explorer, I can’t receive any other verbs but OPTION and PROPFIND. Using TcpTrace, it seems like IIS overrides , and throws a “HTTP/1.1 404 Not Found” messages (in my face).

    I have read both your articles and got loads of help, but I can’t get this to work. I’ve also tried HttpHandler instead of module, but with the same result. I’ve follows your recommendations of IIS configuration and removed all mappings and added aspnet_isapi.dll to the list of wildcards applications. I get the same problems on XP as on 2003 Server.

    I’m a desperate man mr Lane….PLEASE HELP!

    //Mikael

  7. Mikael,
    First thing you should do is go and download litmus which is a functional test suite for WebDAV. If you’re not passing most of those tests, then anything that works is luck.

    Second thing to realize is that Windows Explorer barely follows the WebDAV spec, so getting it to work takes quite some effort. Double check the section called ‘Server Discovery’ on my article with WebDAV tips and tricks. It sounds like the proper OPTIONS might not be being returned from the root of the Web Server. When you create a folder in Explorer using WebDAV the MKCOL command does not get sent until after you name the folder. So, in this case I’m not surprised by what you’re seeing.

    Put some logging code very early in your request handler (like the first line). Although this seems unlikely if you’re getting DavExplorer to work, If you are not getting the request through from IIS then there’s probably one of a few things going on:
    – The mapping is not correct to get all requests handled by your .NET code
    – IIS Request filtering is enabled with something like UrlScan. Look at the IIS logs for something like ‘invalid Verb’.

    Hope that helps.

  8. I’m not comfortable with shell script…Is there a way to run litmus in a windows environment? If so, how do I compile it?

    Thanks

  9. Mortaza,
    Currently non-ASCII letters in URLs are not widely supported. They are also the source of some current security problems with how browsers handle the Unicode conversion. So, in general, I wouldn’t recommend it yet. I have heard (although I don’t know much about it to be honest) that people are trying to push Unicode URLs which makes sense for non-western languages that need the larger character set.

    So, in principle, I’m not opposed to the idea. In practice, it’s currently probably not practical.

  10. Hello everybody, my name is Damion, and I’m glad to join your conmunity,
    and wish to assit as far as possible.

  11. Hii…recently one of my friends had attended an interview and he was asked like “What is the difference between c#.net and asp.net”.and “What are the contents that make up asp.net technology”.Could any one answer me.

  12. Gershom,
    C#.NET is a programming language – really just C#. ASP.NET is web development framework for the .NET platform. .NET supports multiple languages, so you could do ASP.NET programming in C# or VB.NET (among others I assume).

  13. thnx for ur reply.and one more thing “why do we use virtual directory ” and “what is the basic purpose of an IIS”?plz don’t get irritated with my questions.I am having trouble with .NET though i am learning it since 2 months ,i couldn’t be able to figure out like how exactly an application works(i mean ‘right from the request to response,what exactly happens internally’).I get many doubts like why IIS,why “virtual directory”,what is like “finding the path” ,If u can provide a pdf file where the basics are covered clearly(a diagram view would me more better), i would be thankful to you.
    and in ur reply i couldnt be able to convince myself with the definition of asp.net.i mean what contents make up an asp.net other than programming languages and IDEs.
    My ID:- gerel5@yahoo.co.in

  14. Under Windows Server 2003: .* is not allowed in the mapping , there is an extra ‘all extensions’ section where you can add the aspnet_isapi.dll.

    Can you say, how exactly you did this? Some screens or something?

  15. I want to make RewritePath. In IIS 6.0 – OK.
    When run IIS 7.0 – problem ! Why ? I did all chenges in web.config. HELP !!!

  16. Hi,
    I’ve been doing something similar recently: my target is to capture the WebDAV request and write a log into the database (something like an auditlogging feature).
    I’ve been following the steps and in my HttpModule I hooked BeginRequest event to write some text log into a local folder (for experimental purpose). However, when I try to upload a file I got ‘Access Denied’ and there is no log written.
    Any advice on what may be wrong?

  17. Hi,
    I am trying to Configure the WebDav in IIS7 but not able Configure. The same thing is wokring in IIS6 but dont know how it is configured..??
    Any Idea how to set up ?

Leave a Reply

Your email address will not be published. Required fields are marked *