내가 데이터와 코드를 반환 내가 웹 API 컨트롤러가 MVC5 일하고
[Route("m/api/Group")]
[HttpGet]
public IHttpActionResult ListOfGroups()
{
try
{
var listOfGroups = GroupExecutor.GetListOfGroups();
return Ok(listOfGroups);
}
catch (Exception ex)
{
LogClass.Logger.Error(ex.Message, ex);
return InternalServerError();
}
}
다음과 같다 . 이 코드를 실행하면 이제 API 메소드로 이동
[HttpPost]
[ValidateAntiForgeryHeader]
public ActionResult GetListOfGroups()
{
try
{
var listofGroups = new GroupController().ListOfGroups();
return GetJsonContentResult(listofGroups);
}
catch (Exception ex)
{
LogClass.Logger.Error(ex.Message, ex);
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, Utility.GetDescriptionFromEnumValue(Helper.TypeOfError.ErrorOccuredWhileProcessingRequest));
}
}
아래로하지만, 그주는 오류 다시 컨트롤러로 돌아 왔을 때
는 지금은 MVC 컨트롤러 코드를 통해 동일한 프로젝트 내에서이 API 컨트롤러 방법을 소비 할 것입니다
Error getting value from 'Content Negotiation' on 'System.Web.Http.Results.OkNegotiatedContentResult
이 문제를 어떻게 해결할 수 있습니까?
EDIT : 여기
public ContentResult GetJsonContentResult(object data)
{
var camelCaseFormatter = new JsonSerializerSettings();
camelCaseFormatter.ContractResolver = new CamelCasePropertyNamesContractResolver();
var jsonResult = new ContentResult
{
Content = JsonConvert.SerializeObject(data, camelCaseFormatter),
ContentType = "application/json"
};
return jsonResult;
}
왜 내가 컨트롤러에서 이와 같은 웹 API를 호출해야하는지 모르겠습니까? – dotnetstep
MVC5에서 API 컨트롤러를 직접 호출하기 때문에 클라이언트를 만들 필요가 없습니다. – Mahajan344
GetJsonContentResult 메서드는 무엇을합니까? – dotnetstep