I'm currently working with JsonResult. There's a problem: if I call JsonResult in my UserService, I won't be able to call it with a parameter, but if I call it like the same in the UserController, it works with my parameter. So my question now is, the architecture is the same as in the controller so what's wrong?
UserService.cs:
public class UserService : IUserService
{
private readonly IMapper mapper;
private readonly ILogger<UserService> logger;
public UserService(
IMapper mapper,
ILogger<UserService> logger)
{
this.mapper = mapper;
this.logger = logger;
}
private static IList<Contact> GetStaticContacts(string fileName)
{
var jsonText = System.IO.File.ReadAllText(fileName);
var contacts = JsonSerializer.Deserialize<IList<Contact>>(jsonText);
return JsonResult(contacts);
}
Task<IList<Contact>> IUserService.GetNationalCoordinators()
{
return new JsonResult(GetStaticContacts("Test1.json"));
}
Task<IList<Contact>> IUserService.GetLocalCoordinators()
{
return new JsonResult(GetStaticContacts("Test2.json"));
}
Task<IList<Contact>> IUserService.GetMedicalAdvisors()
{
return new JsonResult(GetStaticContacts("Test3.json"));
}
}
UsersController:
public async Task<IActionResult> GetLocalCoordinators(CancellationToken cancellationToken = default)
{
var contacts = await userService.GetLocalCoordinators();
var result = mapper.Map<IList<ContactDto>>(contacts);
return new JsonResult(result);
}
Check this out -> JsonResult in services layer
Also the documentation about JsonResult here https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.jsonresult?view=aspnetcore-6.0 specifies that the JsonResult object comes from ActionResult which is used in the Controller.