Skip to content

Instantly share code, notes, and snippets.

@garchibald
Created September 2, 2012 22:35
Show Gist options
  • Save garchibald/3605243 to your computer and use it in GitHub Desktop.
Save garchibald/3605243 to your computer and use it in GitHub Desktop.
OData AutoMapper Integration
// Grant Archibald (c) 2012
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Query;
using Example.Models;
using Example.DAL;
using Resources.Security;
namespace Example.Controllers
{
[Authorize]
public class MembersController : ApiController
{
private ExampleContext db = new ExampleContext();
//[Queryable]
public IEnumerable<Integration.Member> GetMembers(ODataQueryOptions query)
{
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Member>("Members");
var model = modelBuilder.GetEdmModel();
var mappedQuery = new ODataQueryOptions(new ODataQueryContext(model, typeof(Member)), Request);
var results = new List<Integration.Member>();
foreach ( var result in mappedQuery.ApplyTo(db.Members))
{
results.Add(AutoMapper.Mapper.Map<Integration.Member>(result));
}
return results;
}
// GET api/Member/5
public Integration.Member GetMember(int id)
{
Member member = db.Members.Find(id);
if (member == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return AutoMapper.Mapper.Map<Integration.Member>(member);
}
// PUT api/Member/5
public HttpResponseMessage PutMember(int id, Integration.Member member)
{
if (ModelState.IsValid && id == member.MemberID)
{
var model = AutoMapper.Mapper.Map<Member>(member);
db.Entry(model).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
// POST api/Member
public HttpResponseMessage PostMember(Integration.Member member)
{
if (ModelState.IsValid)
{
db.Members.Add(AutoMapper.Mapper.Map<Member>(member));
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, member);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = member.MemberID }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
// DELETE api/Member/5
public HttpResponseMessage DeleteMember(int id)
{
Member member = db.Members.Find(id);
if (member == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
db.Members.Remove(member);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK, member);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
@codeDeveloper1234
Copy link

When i tried this in my weaapi with odata , results is populated with DTOs but in postman its showing 406 Not Applicable meaning to say that results are not coming in JSon .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment