1

Why can't I return Json. It is underlined and does not exist in the current context.

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.Mvc;

//class and namespace detail removed to keep this post short

    public async Task<IHttpActionResult> Post(string url, StringContent data, Dictionary<string, string> headers)
    {
        using (var client = new HttpClient())
        {
                var response = await client.PostAsync(url, data);
                var result = await response.Content.ReadAsStringAsync();

                //the line below is the error******** 
                return Json(new { HttpStatusCode = HttpStatusCode.OK });
        }
    }

I also tried install-package System.Json and this didn't help.

I'm aware there are probably other things wrong, the code has never worked as I started it an hour ago but I can't understand why Json is not recognized

This is from within a class library (if it matters)

2
  • The method is defined outside any class or namespace!? Commented Sep 2, 2017 at 13:30
  • @PeterBons, I'm sorry. No, I just removed that to keep the post short. Updated to make it clear. Sorry Commented Sep 2, 2017 at 13:31

1 Answer 1

2

That method is a helper method of Web API's ApiController and should be called within a ApiController derived class.

public class MyApiController : System.Web.Http.ApiController {

    public async Task<IHttpActionResult> Post(string url, StringContent data, Dictionary<string, string> headers) {
        using (var client = new HttpClient()) {
            var response = await client.PostAsync(url, data);
            var result = await response.Content.ReadAsStringAsync();

            return Json(new { HttpStatusCode = HttpStatusCode.OK });
        }
    }    
}

The same exists for MVC Controller derived classes as well

public class MyController : Controller {

    public async Task<ActionResult> Post(string url, StringContent data, Dictionary<string, string> headers) {
        using (var client = new HttpClient()) {
            var response = await client.PostAsync(url, data);
            var result = await response.Content.ReadAsStringAsync();

            return Json(new { HttpStatusCode = HttpStatusCode.OK }, JsonRequestBehavior.AllowGet);
        }
    }    
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, and I guess I could return Content if I inherit from Controller then?
@MyDaftQuestions Controller has a Json helper method as well. You should probably update your question to clarify exactly what you are doing. The assumption was web API because of the types used in your example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.