0

I want to return following JSON, but I missed the syntax. How can I correct it?

In my HomeController.cs,

If (result.IsUserActive) return JSON { “UserStatus” : “Active” }

How can I return value “UserStatus” : “Active” In Get method of webapi?

It must be like something:

[HttpGet]
        
        public IHttpActionResult GetUserStatus(int id)
        {
           
            var result = GetStatus(id);    
            if (result.IsUserActive)
            {
                return json ({ “UserStatus” : “Active” });
            }

1 Answer 1

2

You can simply return the Json like:

var result = new {UserStatus="Active" };
return Ok(result);
Sign up to request clarification or add additional context in comments.

2 Comments

if (result.IsUserActive) { var status = new { UserStatus = "Active" }; return ok(status); } This solution is for Web Api 2.
You can also use return Json(status); Returning Json() always returns HTTP 200 and the result in JSON format, no matter what format is in the Accept header of the incoming request. Returning Ok() returns HTTP 200, but the result will be formatted based on what was specified in the Accept request header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.