Create a new Controller:
//[BreezeController]
public class MyController : ApiController
{
//Internal db which we do not expose:
private TodoItemContext _dbContext = new TodoItemContext();
public MyController() {}
[HttpGet]
public string Metadata() {
//Expose our dto Context's metadata:
return MyEFContextProvider.Metadata;
}
// ~/api/my/Todos
[HttpGet]
public IQueryable<Todo> Todos() {
return _dbContext.Set<Models.TodoItem>().Select(x => new Todo {Id = x.TodoItemId, Name = x.Title});
}
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle) {
return _contextProvider.SaveChanges(saveBundle);
}
}
Try hitting the Todos Action:
http://localhost:56216/api/my/metadata
If you get something akin to the following error:
Multiple actions were found that match the request: Metadata on type Spike.Controllers.MyController Todos on type Spike.Controllers.MyController
then you'll have to add an {action} to your routes:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
You should get something akin to:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{"schema":{"namespace":"Spike","alias":"Self","annotation:UseStrongSpatialTypes":"false","xmlns:annotation":"http://schemas.microsoft.com/ado/2009/02/edm/annotation","xmlns:customannotation":"http://schemas.microsoft.com/ado/2013/11/edm/customannotation","xmlns":"http://schemas.microsoft.com/ado/2009/11/edm","cSpaceOSpaceMapping":"[[\"Spike.Todo\",\"Spike.Messages.Todo\"/]]","entityType":{"name":"Todo","customannotation:ClrType":"Spike.Messages.Todo, Spike, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","key":{"propertyRef":{"name":"Id"}},"property":[{"name":"Id","type":"Edm.Int32","nullable":"false","annotation:StoreGeneratedPattern":"Identity"},{"name":"Name","type":"Edm.String","maxLength":"128","fixedLength":"false","unicode":"true"}]},"entityContainer":{"name":"DtoDbContext","customannotation:UseClrTypes":"true","entitySet":{"name":"Todoes","entityType":"Self.Todo"}}}}
</string>