Hello
I'm encountering a 500 server error when setting up a custom api. The goal is to gather custom user info from a quiz and save it into a custom table we created but I may be missing a piece of the puzzle in the setup.
For this an api was registered:
namespace Quiz.App_Start
{
public class QuizRegistration : APIRegistrationBase
{
protected override List<string> ControllerVersions => new List<string>()
{
nameof(QuizResultsController)
};
protected override string CurrentControllerVersion => nameof(QuizResultsController);
protected override string ControllerName => "Quiz";
}
}
The QuizResultsModel.cs:
namespace Quiz.Model
{
public class QuizResultsModel
{
public int UserId { get; set; }
public int Score { get; set; }
public int SuccessCount { get; set; }
public int FailCount { get; set; }
public DateTime DateAttempted { get; set; }
public TimeSpan TimeTaken { get; set; }
}
}
QuizService.cs:
namespace Quiz.Service
{
public class QuizService
{
public static ResponseStatus SaveQuizResult(QuizResultsModel quizResult)
{
ResponseStatus responseStatus = new ResponseStatus();
string sql = @"INSERT INTO QuizResults (UserId, SuccessCount, FailCount, Score, DateAttempted, TimeTaken)
VALUES (@UserId, @SuccessCount, @FailCount, @Score, @DateAttempted, @TimeTaken);
SELECT SCOPE_IDENTITY();";
try
{
using (var connection = new SqlConnection(Database.CreateConnection().ConnectionString))
using (var command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@UserId", quizResult.UserId);
command.Parameters.AddWithValue("@SuccessCount", quizResult.SuccessCount);
command.Parameters.AddWithValue("@FailCount", quizResult.FailCount);
command.Parameters.AddWithValue("@Score", quizResult.Score);
command.Parameters.AddWithValue("@DateAttempted", quizResult.DateAttempted);
command.Parameters.AddWithValue("@TimeTaken", quizResult.TimeTaken);
connection.Open();
var quizResults = command.ExecuteScalar();
responseStatus.HttpStatusCode = HttpStatusCode.OK;
responseStatus.HttpStatusMessage = "Quiz result saved successfully " + quizResults;
}
}
catch (Exception ex)
{
responseStatus.HttpStatusCode = HttpStatusCode.InternalServerError;
responseStatus.HttpStatusMessage = ex.Message;
}
return responseStatus;
}
}
}
QuizResultsController.cs:
namespace Quiz.Controllers
{
[RoutePrefix("someapi/Quiz")]
public class QuizResultsController : ApiController
{
[HttpPost]
[Route("SaveQuizResults")]
public IHttpActionResult SaveQuizResults(QuizResultsModel quizResult)
{
var result = QuizService.SaveQuizResult(quizResult);
return Ok(result);
}
}
}
The client side code that makes the request to the api:
function saveQuizMetricsData(){
fetch('/dwapi/Quiz/SaveQuizResults', {
method: 'POST',
body: JSON.stringify({
QuizId: 1,
UserId: 2,
SuccessCount: 1,
FailCount: 0,
Score: 75,
DateAttempted: '10/23/2024',
TimeTaken: '01:22'
}),
headers: {
'Content-Type': 'application/json'
},
})
.then(response => response.json())
.then((data) => {
console.log(data)
})
.catch(error => {
console.log(error)
})
.finally(() => {
console.log('data saved into database')
});
It's the fetch resource that is causing the 500 server error, so the api may not be properly registered or I haven't set up this correctly and would like to get more insights on how to do it.