logo

C# - How to get data from IHttpActionResult using OkNegotiatedContentResult

Overview: The IHttpActionResult returns a concrete class. The OkNegotiatedContentResult template type defines the concrete class as an actionresult.
  [TestMethod]
   public async Task TestDetails()
	{
	var stringWriter = new StringWriter();
	var httpResponse = new HttpResponse(stringWriter);	
	var httpContext = new HttpContext(httpRequest, httpResponse);

        HttpContext.Current = httpContext;

        MyController myController = new MyController();

	IHttpActionResult actionResult = myController.GetDetails(key);

        var contentResult = actionResult as OkNegotiatedContentResult<DataInfo>;

        DataInfo dataInfo = null;
        dataInfo = contentResult.Content;
	
	Console.WriteLine($"{dataInfo.Field1} {dataInfo.Field2}");
	}

 [Route("Details/{key}"), HttpGet]      
 [ResponseType(typeof(DataInfo))]
 public IHttpActionResult Details(int Key) {
	DataInfo dataInfo = new DataInfo();
	dataInfo.Field1="one";
	dataInfo.Field2="two";
	return Ok(dataInfo);
 }

public class DataInfo { 
        public string Field1 { get; set; }

        public string Field2 { get; set}
}
s