To accomplish it this succinctly I had to build a dynamic javascript page which would allow the intake of an api-key from the users site. Now, I am aware that the big guys at Stack Overflow accomplish this with a JSON object and using
StackOverflow.init({JSON_MADNESS}) kind of tag - but I need this to be fool proof and "pretty" so that someone like my dad wouldn't be afraid of it's length. I think this would go a long way to improving uptake. Also, Shane told me I couldn't do it that way. Lousy EA's.
So I needed to build a script that would output the required javascript to the users page, why the heck not? Here's the (naive) flow of how I tried to accomplish this:- Append a '?' string of variables to the javascript tag. Immediately dismissed as silly after trying to think of how to get the variables from the js file (uh... location? script name? parsing?). If it could be done, I didn't want to waste time coding it into submission.
- Pretty please can I use a JSON object? No? Fine.
- A C# Handler whose call would look like
"ourapi.url.com/JavascriptHandler/MrHudsonsAPIKEY"Beautiful; I'd even used an implemented code with our Google Glass app so I'd have a reference application
- Make sure your project solution has the App_Code folder. If not, simply right-click your project name and find the Add > Add New ASP.Net Folder > App_Code
- Right-click the App_Code folder and select Add > Generic ASP.NET Handler to create a new file. Call it what you feel appropriate; for my project I called it JavascriptHandler
- In the created method stubs, know that
ProcessRequest(HttpContext context)is the called method when the user hits the appropriate URL, so this is where your Handler 'starts.'
If you have IIS6.0 (I did not, but that took a bit of fiddling to find out):
<system.web> <httpHandlers> <add verb="*" path="foo.js" type="JavascriptHandler"/> </httpHandlers></system.web>
If you have IIS7.0 You'll notice that the parent node is
system.webServer this time:<system.webServer>
<handlers>
<add verb="*" path="foo.js"
name="JavascriptHandler"
type="JavascriptHandler"
allowPathInfo="true"/>
</handlers>
</system.webServer>
If you want to know more about what
verb, path, type all mean, read the Microsoft documentation (sorry, can't find the exact link at the moment).I found that if I removed the '.js' file extension, I would get an error page saying "JavascriptHandler not found in this assembly" but it -had- to happen that way... So I queued the music, and got to work.
I eventually found a blog post on exactly what I needed: "Extension-less Routing" This resource and explanation is so well put and apparent (after the fact) that I will leave it up to you to read and enjoy. Spoiler alert: It's a bug in IIS of the mappings of "*." Kind of takes the edge of spending the morning on this.
tl;dr - To get a url like www.example.com/FooHandler/ to run a Handler, go here and apply the hotfix.
Till next time - Find me on Twitter to say hello and ask a question about my ongoing developments