I have been given a requirement to create a web api that fetches information based on an ID and return a set of results. Each result set will be different as the underlying business objects are different. There are too many to have a controller/route for
each one hence the ID parameter on a single route. My problem is that the api renders correctly if I do a separate route/controller for each because I can return an custom object. To use a generic endpoint I thought I could call a business object based on
ID and have each return to me a string that contained the json result which I would turn around and pass to the user inside a generic custom object
public HttpResponseMessage Get(int id)
{
string resultString = string.empty;
IBusinessObject bus = null;
switch(id)
{
case 1:
bus = new firstBusinessObject()
resultString = bus.GetAppStartDetails();
break;
case 2:
bus = new SecondBusinessObject()
resultString = bus.GetAppStartDetails();
break;
}
if(!string.IsNullOrEmpty(resultString))
{
return Request.CreateResponse<ResponseInfo>(HttpStatus.Ok, new ResponseInfo(){AppID = id, AppData = resultString});
Is there any way that I can abstract my data layout away from my api controller to my business objects and just return the result using proper Json format? This is hardly new so there has a to be a best practice way of doing things when the layout varies
- I just don't know how to word my problem.
Conceptually rather than a list of like objects that I am pulling a single result from, I have an object array and each ordinal has a different return. What I want to do is allow for a single entry point to search rather than a separate route for each array
object. I thought that if I serialized all the return data into a Json string and then tried to return that string in a dictionary object from the controller it would work but it made the result look off
You can write a generic Web API just like any generic class or method.
IMHO, the problem is it's hard to make business logic generic unless the application is very basic CRUD operations. Otherwise, you'll end up with delegates or reflections to route to business logic. Also validation and error responses become complicated.
In the end you'll have just as many logical routes as you would Web API end points.
Member
2 Points
3 Posts
Web Api set up/JSON result issue
Feb 08, 2019 05:07 PM|CheeseBread|LINK
Hi
I have been given a requirement to create a web api that fetches information based on an ID and return a set of results. Each result set will be different as the underlying business objects are different. There are too many to have a controller/route for each one hence the ID parameter on a single route. My problem is that the api renders correctly if I do a separate route/controller for each because I can return an custom object. To use a generic endpoint I thought I could call a business object based on ID and have each return to me a string that contained the json result which I would turn around and pass to the user inside a generic custom object
public HttpResponseMessage Get(int id)
{
string resultString = string.empty;
IBusinessObject bus = null;
switch(id)
{
case 1:
bus = new firstBusinessObject()
resultString = bus.GetAppStartDetails();
break;
case 2:
bus = new SecondBusinessObject()
resultString = bus.GetAppStartDetails();
break;
}
if(!string.IsNullOrEmpty(resultString))
{
return Request.CreateResponse<ResponseInfo>(HttpStatus.Ok, new ResponseInfo(){AppID = id, AppData = resultString});
}
else
{
return Request.CreateResponse(HttpStatusCoe.BadRequest,"Error Occurred"):
}
}
// MOdel
public class ResponseInfo
{
public int AppID { get; set; }
public string AppData { get; set; }
public ResponseInfo() { }
}
Business object
do some stuff... The using Newtonsoft.Json convert the custom model to a string
My problem is the result looks similar to this
{
"AppID": 572,
"AppData": "{\"StateList\":[{\"Key\":\"AK\",\"Value\":\"AK\"},{\"Key\":\"AL\",\"Value\":\"AL\"},{\"Key\":\"AR\",\"Value\":\"AR\"},{\"Key\":\"AZ\",\"Value\":\"AZ\"},{\"Key\":\"CA\",\"Value\":\"CA\"},{\"Key\":\"CO\",\"Value\":\"CO\"},{\"Key\":\"CT\",\"Value\":\"CT\"},{\"Key\":\"DC\",\"Value\":\"DC\"},{\"Key\":\"DE\",\"Value\":\"DE\"},{\"Key\":\"FL\",\"Value\":\"FL\"},{\"Key\":\"GA\",\"Value\":\"GA\"},{\"Key\":\"HI\",\"Value\":\"HI\"},{\"Key\":\"IA\",\"Value\":\"IA\"},{\"Key\":\"ID\",\"Value\":\"ID\"},{\"Key\":\"IL\",\"Value\":\"IL\"},{\"Key\":\"KS\",\"Value\":\"KS\"},{\"Key\":\"KY\",\"Value\":\"KY\"},{\"Key\":\"LA\",\"Value\":\"LA\"},{\"Key\":\"MA\",\"Value\":\"MA\"},{\"Key\":\"MD\",\"Value\":\"MD\"},{\"Key\":\"ME\",\"Value\":\"ME\"},{\"Key\":\"MI\",\"Value\":\"MI\"},{\"Key\":\"MN\",\"Value\":\"MN\"},{\"Key\":\"MO\",\"Value\":\"MO\"},{\"Key\":\"NC\",\"Value\":\"NC\"},{\"Key\":\"ND\",\"Value\":\"ND\"},{\"Key\":\"NE\",\"Value\":\"NE\"},{\"Key\":\"NH\",\"Value\":\"NH\"},{\"Key\":\"NM\",\"Value\":\"NM\"},{\"Key\
etc...
which doesn't look correct.
I tried to create an arraylist of List<DictionaryEntry> objects and then let the controller render the result and I got this
{
"AppID": 572,
"AppData": [
[
{
"_key": "AK",
"_value": "AK"
},
{
"_key": "AL",
"_value": "AL"
},
{
"_key": "AR",
"_value": "AR"
},
{
"_key": "AZ",
"_value": "AZ"
},
which wasn't bad until the second list and such got parsed because the next object isn't identified nor is any subsequent items.
{
"_key": "WY",
"_value": "WY"
}
],
[
{
"_key": "Admin",
"_value": "Admin"
},
{
Is there any way that I can abstract my data layout away from my api controller to my business objects and just return the result using proper Json format? This is hardly new so there has a to be a best practice way of doing things when the layout varies - I just don't know how to word my problem.
Thank you.
Member
2 Points
3 Posts
Re: Web Api set up/JSON result issue
Feb 08, 2019 08:11 PM|CheeseBread|LINK
Conceptually rather than a list of like objects that I am pulling a single result from, I have an object array and each ordinal has a different return. What I want to do is allow for a single entry point to search rather than a separate route for each array object. I thought that if I serialized all the return data into a Json string and then tried to return that string in a dictionary object from the controller it would work but it made the result look off
{
"AppID": 572,
"AppData": "{\"StateList\":[{\"Key\":\"AK\",\"Value\":\"AK\"},{\"Key\":\"AL\",\"Value\":\"AL\"},{\"Key\":\"AR\",\"Value\":\"AR\"},{\"Key\":\"AZ\",\"Value\":\"AZ\"},{\"Key\":\"CA\",\"Value\":\"CA\"},{\"Key\":\"CO\",\"Value\":\"CO\"},{\"Key\":\"CT\",\"Value\":\"CT\"},{\"Key\":\"DC\",\"Value\":\"DC\"},{\"Key\":\"DE\",\"Value\":\"DE\"},{\"Key\":\"FL\",\"Value\":\"FL\"},{\"Key\":\"GA\",\"Value\":\"GA\"},{\"Key\":\"HI\",\"Value\":\"HI\"},{\"Key\":\"IA\",\"Value\":\"IA\"},{\"Key\":\"ID\",\"Value\":\"ID\"},{\"Key\":\"IL\",\"Value\":\"IL\"},{\"Key\":\"KS\",\"Value\":\"KS\"},{\"Key\":\"KY\",\"Value\":\"KY\"},{\"Key\":\"LA\",\"Value\":\"LA\"},{\"Key\":\"MA\",\"Value\":\"MA\"},{\"Key\":\"MD\",\"Value\":\"MD\"},{\"Key\":\"ME\",\"Value\":\"ME\"},{\"Key\":\"MI\",\"Value\":\"MI\"},{\"Key\":\"MN\",\"Value\":\"MN\"},{\"Key\":\"MO\",\"Value\":\"MO\"},{\"Key\":\"NC\",\"Value\":\"NC\"},{\"Key\":\"ND\",\"Value\":\"ND\"},{\"Key\":\"NE\",\"Value\":\"NE\"},{\"Key\":\"NH\",\"Value\":\"NH\"},{\"Key\":\"NM\",\"Value\":\"NM\"},{\"Key\
Thanks
All-Star
43761 Points
18740 Posts
Re: Web Api set up/JSON result issue
Feb 08, 2019 08:29 PM|mgebhard|LINK
You can write a generic Web API just like any generic class or method.
IMHO, the problem is it's hard to make business logic generic unless the application is very basic CRUD operations. Otherwise, you'll end up with delegates or reflections to route to business logic. Also validation and error responses become complicated. In the end you'll have just as many logical routes as you would Web API end points.