I can not see any way of including the 'availabilty' and 'dimensions' wrapper elements.
Below is my code from my controller for the existing output
// GET api/product/5
//ProductAvailability_Result is the Complex Type derived from the SP output columns
public IEnumerable<ProductAvailability_Result> Get(int id)
{
myDB_DevEntities db = new myDB_DevEntities();
//ProductAvailability is a SP consisting of a simple 'select' statement that returns the resultset
var Result = db.ProductAvailability(id);
return Result.ToList();
}
Can anyone give any pointers on how to achieve this? And am i approaching this on completly the wrong way by trying to use the above method? It works great until I need to change the structure.
You're returning an enumeration of "ProductAvailability_Result". This class has a defined structure that does not match the one you want. Create a new class that matches the structure and return that class instead of the current one. Obviously you have to
map the result of your stored procedure to your new class, just copy over the values.
Your example of how the structure should look like would match this classes:
class product
{
int productId { get; set; }
availability availability { get; set; }
dimensions { get; set; }
string colour { get; set; }
}
class availability
{
bool inStock { get; set; }
int shelfLevel { get; set; }
int onOrder { get; set; }
}
class dimensions
{
int height { get; set; }
int width { get; set; }
depth { get; set; }
}
Marked as answer by DSB1980 on Feb 07, 2013 06:24 PM
DSB1980
Member
1 Points
6 Posts
MVC API: XML Response with child elements
Feb 05, 2013 03:11 PM|LINK
Hi All,
Very new to MVC. I am currently writing an API and have a strict format that I need the XML to be returned in.
At the moment, I am using my EntityModel to expose my SQL Stored procedure. I have then created an Complex Type for the SP.
I have a controller that is calling the SP and the results are returned in XML.
This fine however, the output is currently (for example):
However, it needs to be structured as:
I can not see any way of including the 'availabilty' and 'dimensions' wrapper elements.
Below is my code from my controller for the existing output
// GET api/product/5 //ProductAvailability_Result is the Complex Type derived from the SP output columns public IEnumerable<ProductAvailability_Result> Get(int id) { myDB_DevEntities db = new myDB_DevEntities(); //ProductAvailability is a SP consisting of a simple 'select' statement that returns the resultset var Result = db.ProductAvailability(id); return Result.ToList(); }Can anyone give any pointers on how to achieve this? And am i approaching this on completly the wrong way by trying to use the above method? It works great until I need to change the structure.
Any advice would be much appreciated.
MartinJ.
Member
243 Points
80 Posts
Re: MVC API: XML Response with child elements
Feb 05, 2013 03:24 PM|LINK
Why don't you declare a structure as you need it and return that from your function? That's the way to go..
DSB1980
Member
1 Points
6 Posts
Re: MVC API: XML Response with child elements
Feb 06, 2013 07:43 AM|LINK
Thanks Martin. Any tips on how to do this?
MartinJ.
Member
243 Points
80 Posts
Re: MVC API: XML Response with child elements
Feb 06, 2013 06:25 PM|LINK
You're returning an enumeration of "ProductAvailability_Result". This class has a defined structure that does not match the one you want. Create a new class that matches the structure and return that class instead of the current one. Obviously you have to map the result of your stored procedure to your new class, just copy over the values.
Your example of how the structure should look like would match this classes:
class product { int productId { get; set; } availability availability { get; set; } dimensions { get; set; } string colour { get; set; } } class availability { bool inStock { get; set; } int shelfLevel { get; set; } int onOrder { get; set; } } class dimensions { int height { get; set; } int width { get; set; } depth { get; set; } }DSB1980
Member
1 Points
6 Posts
Re: MVC API: XML Response with child elements
Feb 07, 2013 06:24 PM|LINK