Search code examples
c#asp.nethandlerashx

Best practices for handling different ajax calls in generic handler


I do a lot of ajax calls in my project, and my generic handlers usually en up pretty messy. Do you guys have any tips on how to manage a lot of calls in the same file? My current solution is to pass a functionname as the first parameter, and then decide on where to end up in the handler based on the name. But this doesnt feel like the best way. Any tips are much appreciated. Thanks


Solution

  • When you want GET information on a resource the server knows about, or to DELETE one, or PUT a new state, then define a URI that refers to it. Querystring parameters are fine for defining just which one (e.g. ?id=34) but not types of resource - that confuses too many separate issues.

    POST handlers shouldn't handle too many different tasks. Ideally one, though maybe stretching to a few more if they are very similar in their operation.

    In other words, Ajax brings nothing new, behind the scenes it's the same web built on HTTP that we had in the 90s. Ajax is just a new way to deal with that.