2

I am currently using context.Request.QueryString within my C# handler (.ashx), because until now I only needed to handle GET posts.

Though what if I am sent a JSON object through POST method? I know I am supposed to deserialize what's sent, but my point is - how can I know if the sending source sent POST or GET.

Why? Because I want to split the handler to POST related functions (things that require security, usually) and to the more primite GET related functions (retrieving public information, etc)

If it matters, my code looks like this now (and it is not ready to handle POSTs properly).

public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "application/json";
    JavaScriptSerializer jss = new JavaScriptSerializer();

    // Wanna know POST was used here, so I can deserialize the sent JSON data
    // ----

    // Handling GET here, good and working
    if (context.Request.QueryString["aname"] != null
        && context.Request.QueryString["type"] != null)
    {
         string adminName = context.Request.QueryString["aname"];
         if(adminName == "test") { // Return some JSON object }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Kfir Eichenblat
  • 449
  • 2
  • 8
  • 27

2 Answers2

5

You have access to context.Request, so you can simply use its HttpMethod property to find out whether it's POST, GET, or something else.

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
3

You can use Request.Form[parameter] for POST. Check out the related post: How to handle C# .NET GET / POST?.

To check whether the request is POST or GET, you can use HttpContext.Current.Request.HttpMethod (from Detect if action is a POST or GET method).

Community
  • 1
  • 1
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80