ASP MVC 5 and Json.NET: action return type

后端 未结 5 2052
闹比i
闹比i 2021-01-19 23:14

i\'m using ASP MVC 5. I have an action in a controller that return a json object:

[HttpGet]
public JsonResult GetUsers()
{
  return Json(....., JsonRequestBe         


        
5条回答
  •  遥遥无期
    2021-01-20 00:11

    public string GetAccount()
    {
        Account account = new Account
        {
            Email = "james@example.com",
            Active = true,
            CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
            Roles = new List
            {
                "User",
                "Admin"
            }
        };
    
        string json = JsonConvert.SerializeObject(account, Formatting.Indented);
    
        return json;
    }
    

    or

    public ActionResult Movies()
    {
        var movies = new List();
    
        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);
    }
    
        

    提交回复
    热议问题