I have this solution (that works), but i would like to know if theres a way to make a loop that checks if the method-name is posted, and if it is -> run the method. Current code:
if (HttpContext.Current.Request["FunctionName"] != null)
{
switch (HttpContext.Current.Request["FunctionName"])
{
case "DoStuff":
DoStuff();
//... etc
Hope you get the idea, otherwise ill elaborate.
Thanks in advance!
You could call
GetType().GetMethod(HttpContext.Current.Request["FunctionName"], new Type[]{})which would return aMethodInfothat you could invoke. I wouldn’t though for a few reasons:The general diciness of do-whatever-the-user-tells-you is high enough that even with the assurance that this was done in a class where every method (including inherited) was safe to run, I’d rather be more active in parsing requests from potentially malicious users.
There’d have to be lot of such methods before the convenience of this outweighed the relative cost, and at that point I’d wonder about the specification of the resource in question. URIs should map to a resource with well defined meanings, rather than including everything but the kitchen sink. There should only be a small number of possible values for the function name anyway.
The title says you’re taking this from the query string, which suggests you’re reacting to a GET by doing different things. GETs should be “look at” operations, that return the state of the thing looked at. This can certainly involve doing quite a bit (classic example is a search that does a lot of complicated comparisons, possibly against a variety of different sources, but is still a “look at” operation). The query string should not select a choice of actions, that should be done by examining the information POSTed to the resource – or better yet POSTed to the resources with completely different URIs for each sort of operation.