JsonResult Class

 

Represents a class that is used to send JSON-formatted content to the response.

Namespace:   System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

Inheritance Hierarchy

System.Object
  System.Web.Mvc.ActionResult
    System.Web.Mvc.JsonResult

Syntax

public class JsonResult : ActionResult
public ref class JsonResult : ActionResult
type JsonResult = 
    class
        inherit ActionResult
    end
Public Class JsonResult
    Inherits ActionResult

Constructors

Name Description
System_CAPS_pubmethod JsonResult()

Initializes a new instance of the JsonResult class.

Properties

Name Description
System_CAPS_pubproperty ContentEncoding

Gets or sets the content encoding.

System_CAPS_pubproperty ContentType

Gets or sets the type of the content.

System_CAPS_pubproperty Data

Gets or sets the data.

System_CAPS_pubproperty JsonRequestBehavior

Gets or sets a value that indicates whether HTTP GET requests from the client are allowed.

System_CAPS_pubproperty MaxJsonLength

Gets or sets the maximum length of data.

System_CAPS_pubproperty RecursionLimit

Gets or sets the recursion limit.

Methods

Name Description
System_CAPS_pubmethod Equals(Object)

(Inherited from Object.)

System_CAPS_pubmethod ExecuteResult(ControllerContext)

Enables processing of the result of an action method by a custom type that inherits from the ActionResult class.(Overrides ActionResult.ExecuteResult(ControllerContext).)

System_CAPS_protmethod Finalize()

(Inherited from Object.)

System_CAPS_pubmethod GetHashCode()

(Inherited from Object.)

System_CAPS_pubmethod GetType()

(Inherited from Object.)

System_CAPS_protmethod MemberwiseClone()

(Inherited from Object.)

System_CAPS_pubmethod ToString()

(Inherited from Object.)

Remarks

You can return an instance of the JsonResult class by using the Json method.

Examples

The following example shows how to return an instance of the JsonResult class from an action method. The object that is returned specifies that a GET request is permitted.

public ActionResult Movies()
{
    var movies = new List<object>();

    movies.Add(new { Title = "Ghostbusters", Genre = "Comedy", Year = 1984 });
    movies.Add(new { Title = "Gone with Wind", Genre = "Drama", Year = 1939 });
    movies.Add(new { Title = "Star Wars", Genre = "Science Fiction", Year = 1977 });

    return Json(movies, JsonRequestBehavior.AllowGet);
}

The next example shows how to retrieve and display the JSON-formatted content.

<input name="btnGetMovies" id="btnGetMovies" type="submit" value="Get Movies">
<ul id="movieList"></ul>

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
    $("#btnGetMovies").click(function () {
        var actionUrl = '@Url.Action("Movies", "Home")';
        $.getJSON(actionUrl, displayData);
    });

    function displayData(response) {
        if (response != null) {
            for (var i = 0; i < response.length; i++) {
                $("#movieList").append("<li>" + response[i].Title + " " + response[i].Genre + " " + response[i].Year + "</li>")
            }
        }
    }
</script>

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

System.Web.Mvc Namespace

Return to top