Trying to do REST on IIS using ASP.NET, with some success so far.
The task is to create a shared storage area for scanning jobs.I started out with a set of types, in Haskell notation:
CreateJob :: IO (Status, Location)
UploadFile :: Job ID -> File -> IO (Status, Location)
ModifyFile :: Job ID -> File ID -> File -> IO Status
RetrieveFile :: Job ID -> File ID -> IO (Status, File)
DeleteFile :: Job ID -> File ID -> IO Status
ListFiles :: Job ID -> IO (Status, [Location])
ListJobs :: IO (Status, [Location])
DeleteJob :: Job ID -> IO Status
The IO tag tells us that the value is created by some I/O operation on the server, but not much else - it’s not obvious which functions are idempotent, and which aren’t, for instance. It strikes me that a proper Haskell DSEL for REST notation could capture that information, and much else besides. But then, before you knew it, you’d be writing your web applications in Haskell.
Anyway, manually translating the above into HTTP requests addressed to URLs, it breaks down fairly cleanly into the following:
- CreateJob
- POST to /, (successful) status=201, location=/JobId
- UploadFile
- POST File to /Job_ID, status=201, location= /Job_ID/File_ID
- Modify File
- PUT File to /Job_ID/File_ID, status=204
- Retrieve File
- GET to /Job_ID/File_ID, status=200, content=File
- Delete File
- DELETE to /Job_ID/File_ID, status=204
- List Files
- GET to /Job_ID, status=200
- Delete Job
- DELETE to /Job_ID, status=204
The next task was to get the URL rewriting working. I decided to use a custom IHttpHandler (very much like a Resource in ), and ensure that it was called by appending /handler.aspx to every URL.
That’s pretty much all there is to it, apart from writing the actual code to perform the requested operations on the server. Because it’s on IIS, we can use Windows authentication for security (yes, yes, I know…quit smirking…). I’ve heard it said that you can use WSDL to describe RESTful web services, but I don’t wanna. Something like the Haskell notation alluded to above would be much cooler.
The lists of URLs returned by ListJobs and ListFiles are formatted as plaintext separated by newlines. (I was originally going to use RDF lists encoded in RDF/XML, but none of the arguments I came up with for which this was better seemed terribly convincing to me).