[英]How can I return a JSON object from ASP.Net WebAPI?
I want to return something like this but it doesn't work: 我想返回这样的东西,但它不起作用:
if (user == null)
{
return Ok({name: true});
}
return Ok({name: false);
Can someone tell me how I can make it return a value of true of false for a element "name" from my action method. 有人可以告诉我如何使我的操作方法为元素“名称”返回true或false值。
If you are using Web API, create a class to have your property Name: 如果您使用的是Web API,请创建一个具有您的属性Name的类:
public class NameResponse {
public bool Name { get; set; }
}
And return the JSON like this: 然后像这样返回JSON:
Request.CreateResponse<NameResponse>(HttpStatusCode.OK, new NameResponse() { Name = true });
If you're not using Web API and using a normal Controller you need to do this: 如果您没有使用Web API,而是使用普通的Controller,则需要执行以下操作:
if (user == null)
{
return Json(new { name = true });
}
return Json(new { name = false });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.