Hoping someone can hepl with an issue I've been having. I've created a Web API that fetches records from the a database. I have the following functions in place which work ok:
localhost:8080/api/Budgets -- Returns all Budgets
localhost:8080/api/Budgets(799) -- Returns all for contract 799
What I'm looking for is to be able to pass in two parameters to allow me to do calulations on the records retrieved so something like:
localhost:8080/api/Budgets(799)/215 -- Where 799 is the contract Number and 215 is a material number.
This will allow me to return a dataset of only materials for that budget that I can then do calculations on. There is only one SQL table for this which contains both the contract number and material number as well as a load of other figures.
Here's what I have so far but it doesnt seem to be working:
public IHttpActionResult Get()
{
return Ok(context.Budgets);
}
public IHttpActionResult Get([FromODataUri] int key)
{
var budgets = context.Budgets.FirstOrDefault(p => p.ProjectNo == key);
if (budgets == null)
{
return NotFound();
}
return Ok(budgets);
}
[ODataRoute("GetMaterialUsage(ProjectId={ProjectId},ResourceCode={ResourceCode})")]
public IHttpActionResult GetMaterialUsage([FromODataUri] int ProjectId, [FromODataUri] string ResourceCode)
{
var budgets = context.Budgets.FirstOrDefault(p => p.ProjectNo == ProjectId && p.ResourceCode == ResourceCode);
if (budgets == null)
{
return NotFound();
}
return Ok(budgets);
}
WebApiConfig:
ODataModelBuilder builder = new ODataConventionModelBuilder();
// Web API configuration and services
builder.EntitySet<BudgetTypes>("BudgetTypes");
builder.EntitySet<Budgets>("Budgets");
var function = builder.Function("GetMaterialUsage");
function.Parameter<int>("ProjectId");
function.Parameter<string>("ResourceCode");
function.ReturnsCollectionFromEntitySet<Budgets>("Budgets");
// Web API routes
config.MapHttpAttributeRoutes();
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "api",
model: builder.GetEdmModel()
);
Can anyone shine some light on how this can be done? I just want to pass 2 parameters to a function from the url.
At the moment the following links return 404: localhost:8080/api/Budgets(752,230) localhost:8080/api/Budgets(ProjectNo=752,ResourceCode=230)