6

I'm trying to return a Json string from an MVC controller in a WebAPI application, but am unable to use return Json(... because the class being used extends ApiController and not Controller (I believe).

Is there an alternative method to do what I'm trying to do (e.g. return a different type)? Or a workaround?

This is my controller code:

public class SocialController : ApiController 
{
    public ActionResult Get(SocialRequest request) // or JsonResult?
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        string jsontest = js.Serialize(request); // just serializing back and forth for testing

        return Json(jsontest, JsonRequestBehavior.AllowGet);
    }
}

The error I'm receiving is "System.Web.Helpers.Json is a type but is used like a variable."

I've found the following related SO question but it hasn't solved it for me, if anyone can elaborate I'd really appreciate it (and dish out the rep points): Why Json() Function is unknown

1 Answer 1

9

In Asp.net Web Api you don't have ActionResults anymore. You simply return the object you need. The framework converts the object to the proper response (json, xml or other types)

[HttpGet]
public IEnumerable<string> GetUsers(SocialRequest request)
{
    return _applicationsService.GetUserss(request);
}
Sign up to request clarification or add additional context in comments.

Comments

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.