Edward Mahoney

Edward Mahoney

  • NA
  • 10
  • 5.1k

Return from ASP.NET Core Web API a dictionary, not List

Mar 11 2021 12:00 AM
I found your article below excellent. Great job.
 
https://www.c-sharpcorner.com/article/apiasp-net-core-web-api-entity-framewor-call-stored-procedure-part-ii/
 
My question is how do I get a non list JSON response returned. The person consuming the json wants a dictionary, not a list. My stored proc returns one row so no list is needed.
 
So from your example I want
  1. {  
  2. "appointmentId": 2,  
  3. "returnCode": 1,  
  4. "submittedTime""2020-12-06T15:19:10.87"  
  5. }  
not this:
  1. [  
  2. {  
  3. "appointmentId": 2,  
  4. "returnCode": 1,  
  5. "submittedTime""2020-12-06T15:19:10.87"  
  6. }  
  7. ]  
  1. public async Task<ActionResult<IEnumerable<output>>> Getoutput(Input input)  
  2. {  
  3. string StoredProc = "exec CreateAppointment " +  
  4. "@ClinicID = " + input.ClinicId + "," +  
  5. "@AppointmentDate = '" + input.AppointmentDate + "'," +  
  6. "@FirstName= '" + input.FirstName + "'," +  
  7. "@LastName= '" + input.LastName + "'," +  
  8. "@PatientID= " + input.PatientId + "," +  
  9. "@AppointmentStartTime= '" + input.AppointmentStartTime + "'," +  
  10. "@AppointmentEndTime= '" + input.AppointmentEndTime + "'";  
  11. //return await _context.output.ToListAsync();  
  12. return await _context.output.FromSqlRaw(StoredProc).ToListAsync();  
  13. }

Answers (4)