Created
January 6, 2017 07:50
-
-
Save ridomin/6e7b03db77b3667de63835ec9aa8cc50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Http.Controllers; | |
using System.Web.Http.Filters; | |
namespace ZumoExtensions | |
{ | |
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | |
public class ExpandPropertyAttribute : ActionFilterAttribute | |
{ | |
string propertyName; | |
public ExpandPropertyAttribute(string propertyName) | |
{ | |
this.propertyName = propertyName; | |
} | |
public override void OnActionExecuting(HttpActionContext actionContext) | |
{ | |
base.OnActionExecuting(actionContext); | |
var uriBuilder = new UriBuilder(actionContext.Request.RequestUri); | |
var queryParams = uriBuilder.Query.TrimStart('?').Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries).ToList(); | |
int expandIndex = -1; | |
for (var i = 0; i < queryParams.Count; i++) | |
{ | |
if (queryParams[i].StartsWith("$expand", StringComparison.Ordinal)) | |
{ | |
expandIndex = i; | |
break; | |
} | |
} | |
if (expandIndex < 0) | |
{ | |
queryParams.Add("$expand=" + this.propertyName); | |
} | |
else | |
{ | |
queryParams[expandIndex] = queryParams[expandIndex] + "," + propertyName; | |
} | |
uriBuilder.Query = string.Join("&", queryParams); | |
actionContext.Request.RequestUri = uriBuilder.Uri; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment