Created
May 4, 2016 23:42
-
-
Save crmckenzie/67b8741e3ae5cea02e67fb1583a05275 to your computer and use it in GitHub Desktop.
Web Api + OData + AutoMapper + HyperMedia
This file contains 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
[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