簡體   English   中英

JsonResult 不包含帶 1 個參數的構造函數

[英]JsonResult does not contain a constructor that takes 1 argument

我目前正在使用 JsonResult。 有一個問題:如果我在我的 UserService 中調用 JsonResult,我將無法使用參數調用它,但如果我在 UserController 中調用它,它可以使用我的參數。 所以我現在的問題是,架構與 controller 中的架構相同,所以出了什么問題?

用戶服務.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"));
    }
}

用戶控制器:

public async Task<IActionResult> GetLocalCoordinators(CancellationToken cancellationToken = default)
    {
            var contacts = await userService.GetLocalCoordinators();
            var result = mapper.Map<IList<ContactDto>>(contacts);
            return new JsonResult(result);
    }

檢查一下 -> 服務層中的 JsonResult

此外,有關 JsonResult 的文檔https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.jsonresult?view=aspnetcore-6.0指定 JsonResult object 來自 ActionResult 用於Controller。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM