Skip to content

Instantly share code, notes, and snippets.

@crmckenzie
Created May 4, 2016 23:42
Show Gist options
  • Save crmckenzie/67b8741e3ae5cea02e67fb1583a05275 to your computer and use it in GitHub Desktop.
Save crmckenzie/67b8741e3ae5cea02e67fb1583a05275 to your computer and use it in GitHub Desktop.
Web Api + OData + AutoMapper + HyperMedia
[System.Web.Http.HttpGet]
[System.Web.OData.EnableQuery]
[System.Web.Http.Route("~/api/v1/widgets/query", Name = "widgets-query")]
[ResponseType(typeof(List<widgetJson>))]
public IQueryable<widgetJson> Query(ODataQueryOptions<WidgetEntity> query)
{
var odata = query.ApplyTo(_dbContext.widgets)
.Cast<WidgetEntity>()
.ToList()
;
var json = odata.AsQueryable()
.ProjectTo<WidgetJson>(AutoMapperConfiguration)
.ToList()
;
json.ForEach(MapUrls);
return json.AsQueryable();
}
private void MapUrls(WidgetJson Widget)
{
var helper = new UrlHelper(requestContext: HttpContext.Current.Request.RequestContext, routeCollection: RouteTable.Routes);
dynamic urls = new ExpandoObject();
urls.edit = helper.ActionAbsolute(MVC.Widgets.Edit(Widget.Name));
urls.details = helper.ActionAbsolute(MVC.Widgets.Details(Widget.Name));
Widget.Urls = urls;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment