How to get "Query String", "Headers" and "Body" from HttpActionExecutedContext

In Web API for Exception Handling we use ExceptionFilter to override OnException event of ExceptionFilterAttribute class. We need to use HttpActionExecutedContext to get current context of application.

In some time we need "Query String", "Headers" and "Body" from HttpActionExecutedContext. Query String and Headers easily accessible from HttpActionExecutedContext .
But Body is not easily accessible you need to use context.Request.ReadAsStringAsync() and reset the position in the stream to zero and then try to access it.

I am giving code snippet for these

?
01
02
03
04
05
06
07
08
09
10
11
public class ExceptionFilter: ExceptionFilterAttribute {
  public override void OnException(HttpActionExecutedContext context) {
      WebAPIRequestHelper request = new WebAPIRequestHelper();
      var requestQueryString = request.GetQueryStringFromRequest(context);
      var requestBody = request.GetBodyFromRequest(context);
      var requestHeaders = request.GetHeadersFromRequest(context);
  }
}
public class ExceptionFilter: ExceptionFilterAttribute {

  public override void OnException(HttpActionExecutedContext context) {
      WebAPIRequestHelper request = new WebAPIRequestHelper();

      var requestQueryString = request.GetQueryStringFromRequest(context);
      var requestBody = request.GetBodyFromRequest(context);
      var requestHeaders = request.GetHeadersFromRequest(context);
  }

}

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//WebAPI Request Helper class is use to get "Query String","Headers" and "Body" from HttpActionExecutedContext
 
public class WebAPIRequestHelper {
    public object GetQueryStringFromRequest(HttpActionExecutedContext context) {
        var queryStringList = new List < KeyValuePair < string,
            string >> ();
 
        if (context.Request.RequestUri != null) {
            var queryCollection = context.Request.RequestUri.Query.Substring(1).Split('&');
 
            foreach(var item in queryCollection) {
                var itm = item.Split('=');
                var queryString = new KeyValuePair < string,
                    string > (itm[0], itm[1]);
                queryStringList.Add(queryString);
            }
        }
 
        return queryStringList;
    }
 
    public List < KeyValuePair < string, string >> GetHeadersFromRequest(HttpActionExecutedContext context) {
        var headersList = new List < KeyValuePair < string,
            string >> ();
 
        foreach(var header in context.Request.Headers) {
            headersList.Add(new KeyValuePair < string, string > (header.Key, ((string[])(header.Value))[0]));
        }
 
        return headersList;
    }
    public string GetBodyFromRequest(HttpActionExecutedContext context) {
        string data;
        using(var stream = context.Request.Content.ReadAsStreamAsync().Result) {
            if (stream.CanSeek) {
                stream.Position = 0;
            }
            data = context.Request.Content.ReadAsStringAsync().Result;
        }
        return data;
    }
}
 
//WebAPI Request Helper class is use to get "Query String","Headers" and "Body" from HttpActionExecutedContext 

public class WebAPIRequestHelper {
    public object GetQueryStringFromRequest(HttpActionExecutedContext context) {
        var queryStringList = new List < KeyValuePair < string,
            string >> ();

        if (context.Request.RequestUri != null) {
            var queryCollection = context.Request.RequestUri.Query.Substring(1).Split('&');

            foreach(var item in queryCollection) {
                var itm = item.Split('=');
                var queryString = new KeyValuePair < string,
                    string > (itm[0], itm[1]);
                queryStringList.Add(queryString);
            }
        }

        return queryStringList;
    }

    public List < KeyValuePair < string, string >> GetHeadersFromRequest(HttpActionExecutedContext context) {
        var headersList = new List < KeyValuePair < string,
            string >> ();

        foreach(var header in context.Request.Headers) {
            headersList.Add(new KeyValuePair < string, string > (header.Key, ((string[])(header.Value))[0]));
        }

        return headersList;
    }
    public string GetBodyFromRequest(HttpActionExecutedContext context) {
        string data;
        using(var stream = context.Request.Content.ReadAsStreamAsync().Result) {
            if (stream.CanSeek) {
                stream.Position = 0;
            }
            data = context.Request.Content.ReadAsStringAsync().Result;
        }
        return data;
    }
}

Previous
Next Post »
Thanks for your comment