I'm fairly new to Web APIs and although I've read the responses for similar issues faced, I'm still sitting with errors.
I'm using VS 2019, MS SQL Enterprise, and Postman to pass the Json file to the endpoint.
Even with the below test code it still throws 500 internal errors (obviously an unhandled error). I've been pulling my hair out, and I'm sure it's probably a simple error which I can't see
NB: When I deploy to the server, the website is being hit according to IIS logs
VS Code
public class DefaultController : ApiController
{
public string Get()
{
using (DynatraceEntitiesPerson db = new DynatraceEntitiesPerson())
{
var listPerson = db.PersonTests.ToList();
var strJson = JsonConvert.SerializeObject(listPerson);
return strJson;
}
}
public HttpResponseMessage Post([FromBody] object personTest)
{
try
{
var JsonPerson = JsonConvert.DeserializeObject<PersonTest>(personTest.ToString());
using (DynatraceEntitiesPerson db = new DynatraceEntitiesPerson())
{
db.PersonTests.Add(JsonPerson);
db.SaveChanges();
db.PersonTests.ToList();
}
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest,"error " + ex.InnerException);
}
return Request.CreateResponse(HttpStatusCode.OK,"person added");
}
}
Model based on DB
namespace Test.Models
{
using System;
using System.Collections.Generic;
public partial class PersonTest
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
}
}
POSTMAN Body | Raw | Json Format
{
"Name": "3",
"Surname": "tester3"
}
MS SQL table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PersonTest](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](20) NULL,
[Surname] [varchar](20) NULL,
CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO





