简体   繁体   English

IActionResult 传递 object 并在 Ok 中发送消息

[英]IActionResult pass object and message in Ok

In .NET 6 API controller, I'm returning data with Ok IActionResult , below is my code, there I want to pass a string message along with return Ok(employee);在 .NET 6 API controller 中,我用 Ok IActionResult返回数据,下面是我的代码,我想在return Ok(employee);

Is it possible if so, please suggest how to do it.如果可以的话,请建议如何去做。

[Route("{Id}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Employee))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetEmployeeDetails(int Id)
{
    var listEmployees = new List<Employee>()
                    {
                        new Employee(){ Id = 1001, Name = "Anurag", Age = 28, City = "Mumbai", Gender = "Male", Department = "IT" },
                        new Employee(){ Id = 1002, Name = "Pranaya", Age = 28, City = "Delhi", Gender = "Male", Department = "IT" },
                        new Employee(){ Id = 1003, Name = "Priyanka", Age = 27, City = "BBSR", Gender = "Female", Department = "HR"},
                    };
    var employee = listEmployees.FirstOrDefault(emp => emp.Id == Id);
    if (employee != null)
    {
        **return Ok(employee);**
    }
    else
    {
        return NotFound();
    }
}

In order to return the message with the data as well, I would suggest creating a model class that allows returning multiple values.为了也返回带有数据的消息,我建议创建一个允许返回多个值的 model class。

For example, an ApiResponse class that support generic type.例如,支持通用类型的ApiResponse class。

public class ApiResponse<T>
{
    public string Message { get; set; }

    public T Data { get; set; } 
}

And your controller action to return the value of ApiResponse<Employee> type for the status 200 scenario as below:以及您的 controller 操作返回状态 200 场景的ApiResponse<Employee>类型的值,如下所示:

[Route("{Id}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ApiResponse<Employee>))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetEmployeeDetails(int Id)
{
    var listEmployees = new List<Employee>()
    {
        new Employee(){ Id = 1001, Name = "Anurag", Age = 28, City = "Mumbai", Gender = "Male", Department = "IT" },
        new Employee(){ Id = 1002, Name = "Pranaya", Age = 28, City = "Delhi", Gender = "Male", Department = "IT" },
        new Employee(){ Id = 1003, Name = "Priyanka", Age = 27, City = "BBSR", Gender = "Female", Department = "HR"},
    };
    var employee = listEmployees.FirstOrDefault(emp => emp.Id == Id);
    if (employee != null)
    {
        return Ok(new ApiResponse<Employee>
        {
            Message = "Assign your message here",
            Data = employee
        });
    }
    else
    {
        return NotFound();
    }
}

Unfortunately don't think that is possible to do.不幸的是,不认为这是可能的。 The workaround could be that you add an extra field to the Employee object such as a string of description that gets returned as a whole.解决方法可能是您向 Employee object 添加一个额外的字段,例如作为一个整体返回的描述字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM