使用过滤器i';将Linq语句添加到Web API Get;我正在尝试添加$select
我正在尝试将一些linq语句应用于所有Get Web api命令。我想我可以使用使用过滤器i';将Linq语句添加到Web API Get;我正在尝试添加$select,linq,asp.net-web-api,odata,Linq,Asp.net Web Api,Odata,我正在尝试将一些linq语句应用于所有Get Web api命令。我想我可以使用actionfilteratAttribute来实现这一点 我基本上是在WebAPI中添加$select支持,因为它目前不受支持。我不确定在哪里可以获得IQueryable结果。我相信在sql执行之前需要它,但在Get函数返回IQueryable结果之后需要它。任何帮助都会很好。我正在尝试类似的方法,但他的想法行不通,因为httpresponsemessageresponse=actionexecutecontext
actionfilteratAttribute
来实现这一点
我基本上是在WebAPI中添加$select支持,因为它目前不受支持。我不确定在哪里可以获得IQueryable
结果。我相信在sql执行之前需要它,但在Get函数返回IQueryable
结果之后需要它。任何帮助都会很好。我正在尝试类似的方法,但他的想法行不通,因为httpresponsemessageresponse=actionexecutecontext.Result代码>不再在RC中
谢谢
尼克
解决方案
见上面的原始帖子。我在底部添加了解决方案。我已经构建了一个解决方案,明天我将在清理代码后发布它。
public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
{
HttpRequestMessage request = actionExecutedContext.Request;
HttpResponseMessage response = actionExecutedContext.Response;
IQueryable obj;
if (response != null && response.TryGetContentValue(out obj) && request.RequestUri.ParseQueryString()["$select"] != null)
{
System.Collections.Specialized.NameValueCollection QueryItems = request.RequestUri.ParseQueryString();
string select = QueryItems["$select"];
if (!string.IsNullOrWhiteSpace(select))
{
obj = obj.Select(string.Format("new ({0})", select));
}
//
//this should be generic not hard coded for Json
//
string json = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse();
actionExecutedContext.Response.Content = new StringContent(json);
actionExecutedContext.Response.Content.Headers.Clear();
actionExecutedContext.Response.Content.Headers.Add("Content-Type", "application/json");
actionExecutedContext.Response.StatusCode = System.Net.HttpStatusCode.OK;
}
}