Skip to content

Commit ac7c39c

Browse files
committedMar 10, 2016
Merge pull request #50 from david-hollifield/master
Check for non-null Response in OnActionExecuted
2 parents 5fd8378 + a8e5510 commit ac7c39c

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed
 

‎LinqToQuerystring.WebApi/LinqToQueryableAttribute.cs

+30-27
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,41 @@ public LinqToQueryableAttribute(bool forceDynamicProperties = false, int maxPage
2222

2323
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
2424
{
25-
object responseObject;
26-
27-
actionExecutedContext.Response.TryGetContentValue(out responseObject);
28-
var originalquery = responseObject as IQueryable;
29-
30-
if (originalquery != null)
25+
if (actionExecutedContext.Response != null)
3126
{
32-
var queryString = actionExecutedContext.Request.RequestUri.Query;
33-
var genericType = originalquery.GetType().GetGenericArguments()[0];
27+
object responseObject;
3428

35-
var reply = originalquery.LinqToQuerystring(genericType, queryString, this.forceDynamicProperties, this.maxPageSize);
36-
var replyType = reply.GetType();
29+
actionExecutedContext.Response.TryGetContentValue(out responseObject);
30+
var originalquery = responseObject as IQueryable;
3731

38-
if (typeof(IQueryable).IsAssignableFrom(replyType))
32+
if (originalquery != null)
3933
{
40-
var queryableType = typeof(IQueryable<>).GetGenericTypeDefinition();
41-
var genericArgs = replyType.GetGenericArguments();
42-
replyType = queryableType.MakeGenericType(genericArgs);
34+
var queryString = actionExecutedContext.Request.RequestUri.Query;
35+
var genericType = originalquery.GetType().GetGenericArguments()[0];
36+
37+
var reply = originalquery.LinqToQuerystring(genericType, queryString, this.forceDynamicProperties, this.maxPageSize);
38+
var replyType = reply.GetType();
39+
40+
if (typeof(IQueryable).IsAssignableFrom(replyType))
41+
{
42+
var queryableType = typeof(IQueryable<>).GetGenericTypeDefinition();
43+
var genericArgs = replyType.GetGenericArguments();
44+
replyType = queryableType.MakeGenericType(genericArgs);
45+
}
46+
47+
var configuraton = actionExecutedContext.ActionContext.ControllerContext.Configuration;
48+
var conneg = (IContentNegotiator) configuraton.Services.GetService(typeof(IContentNegotiator));
49+
var formatter = conneg.Negotiate(
50+
replyType,
51+
actionExecutedContext.Request,
52+
configuraton.Formatters);
53+
54+
actionExecutedContext.Response = new HttpResponseMessage
55+
{
56+
StatusCode = HttpStatusCode.OK,
57+
Content = new ObjectContent(replyType, reply, formatter.Formatter)
58+
};
4359
}
44-
45-
var configuraton = actionExecutedContext.ActionContext.ControllerContext.Configuration;
46-
var conneg = (IContentNegotiator)configuraton.Services.GetService(typeof(IContentNegotiator));
47-
var formatter = conneg.Negotiate(
48-
replyType,
49-
actionExecutedContext.Request,
50-
configuraton.Formatters);
51-
52-
actionExecutedContext.Response = new HttpResponseMessage
53-
{
54-
StatusCode = HttpStatusCode.OK,
55-
Content = new ObjectContent(replyType, reply, formatter.Formatter)
56-
};
5760
}
5861
}
5962
}

0 commit comments

Comments
 (0)
Please sign in to comment.