728x90

C#에서 JSON데이터를 다루기위해 JObject라는 클래스를 사용하였다.

 

ASP.NET MVC 프레임워크를 사용한다면, JsonResult와 ActionResult도 사용이 가능하다. 주로 컨트롤러의 메서드에서 jsonresult를 반환하여 클라이언트에게 json데이터를 전송할 때 사용한다.

 

MVC 패턴을 사용하기 위해서 아래 내용이 임포트 되어있어야 한다.

using
Microsoft.AspNetCore.Mvc;

 

JsonResult

 ASP.NET MVC에서 사용되는 클래스로, JSON 형식의 데이터를 반환하기 위해 사용 주로 컨트롤러의 액션 메서드에서 JsonResult를 반환하여 클라이언트에게 JSON 데이터를 전송할 수 있다.

using Microsoft.AspNetCore.Mvc;

public class MyController : Controller
{
    public JsonResult MyAction()
     {
     var data = new { name = "John", age = 30 };
     return Json(data);
     }
}

 

ActionResult

ActionResult는 ASP.NET MVC에서 액션 메서드의 결과를 나타내는 추상 클래스

ActionResult를 상속하여 다양한 종류의 결과를 반환한다. JsonResult은 ActionResult의 일종입니다.

JsonResult와 다른 점은 ActionResult는 다른 유형의 결과도 반환할 수 있으며, 예를 들어 ViewResult는 HTML 뷰를 반환하고, RedirectResult는 다른 URL로 리디렉션할 수 있다.

using Microsoft.AspNetCore.Mvc;

public class MyController : Controller
{
    public ActionResult MyAction()
     {
     var data = new { name = "John", age = 30 };
     return Json(data);
     // return View(); // HTML 뷰 반환
    // return RedirectToAction("Index", "Home"); // 다른 액션으로 리디렉션
     }
}

 

728x90

'C# asp.net' 카테고리의 다른 글

ASP.NET 웹 폼 특징  (1) 2024.01.03
C# DB 연결관련 메소드 정리  (0) 2023.08.01
C# Newtonsoft.Json, JObject  (0) 2023.06.26
C# namespace, using 사용법  (0) 2023.06.19
C# : Database 연동(SqlClient 사용)  (0) 2022.04.21

+ Recent posts