Mahajan344
Mahajan344

Reputation: 2550

Consuming IHttpActionResult from MVC controller

I am working on MVC5 where I have web Api controller which is returning data and code is as below

[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();
            }
        }

If I run this url in brower ,its returning data correctly in json format.

Now I want consume this API controller method within same project through MVC controller and code is as below

[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));
            }
        }

Now When I run this code goes to api method but when returned back to controller ,its giving error

Error getting value from 'Content Negotiation' on 'System.Web.Http.Results.OkNegotiatedContentResult

How can I solve this issue ?

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;
        }

Upvotes: 0

Views: 1548

Answers (2)

dotnetstep
dotnetstep

Reputation: 17485

I hope that your web api always have consistent in returning result.

I assume that it alway

Ok(yourdata)

Now your GetJsonContentResult Method should modified following way. In this your WebAPI return OkNegotiatedContentResult and from that you have to read Content property. That I have done with reflection. ( There are others way to do it).

 public ContentResult GetJsonContentResult(object data)
        {            
            var camelCaseFormatter = new JsonSerializerSettings();
            camelCaseFormatter.ContractResolver = new CamelCasePropertyNamesContractResolver();
            PropertyInfo pinfo =  data.GetType().GetProperty("Content");            
            var jsonResult = new ContentResult
            {

                Content = JsonConvert.SerializeObject(pinfo.GetValue(data) , camelCaseFormatter),
                ContentType = "application/json"
            };
            return jsonResult;
        }

Upvotes: 1

Atul Chaudhary
Atul Chaudhary

Reputation: 3736

 var controller = new MyAPIController();
 var content = controller.GetMyAPIMethod(someParam);
            var myModel = ((OkNegotiatedContentResult<MyObjectType>)(content.Result)).Content;

Here MyAPIController is ur API Controller, GetMyAPIMethod is ur API method and MyObjectType is data type it is returning. Use this in your controller and this should work

Upvotes: 0

Related Questions