我正在使用WebAPI,在那里我想要从单一方法返回具有不同结果的对象。 EG。让我们学生的一个例子在c中返回来自同一对象的混合结果#
对象/类:
public class student
{
//Some properties
}
public class error
{
//Some properties
}
public class ResponseObject
{
//Some properties
public student stud {get; set;}
public error err {get; set;}
}
方法:
public ResponseObject StudentsDetail(string a, string b)
{
//Some code
}
现在在这里,我想给学生的详细信息,如果调用全成 否则错误信息。
每当我发送响应时,ResponseObject对象都包含类student和error的所有属性。
我想发送其中的一个。 我该如何做到这一点? 依赖注入可能吗? 或者以其他方式暗示我。
欲了解更多的澄清,我在这里更新我的问题。 响应样本会是这样
成功JSON响应
{
"title": "Student",
"message": "success",
"student": {
"id": {
"name": "abc",
"add": "xyz"
}
}
}
错误JSON响应
{
"title": "Student",
"message": "error",
"error": {
"message": {
"text": "asdlkj"
}
}
}
那么将其更改为void方法,然后将Student和Error作为第三个和第四个参数传递? – RizkiDPrast
那我该如何回应我的要求呢? – user711241