0

I am trying to create a basic test web api, and use a standard controller to test call it. When I run it, by putting http://localhost:55144/home/testapi

it'll run the catcher function and completely ignore the parameter. Then, the catcher will happily return a value, which can be seen in the calling code.

I have tried various combinations of putting [FromBody], changing the type of the parameter in TestApiMethod, and seeing if making a list or array makes any difference.

I've noticed a couple of weird things: - I'm not using the parameter in the code of TestApiMethod, but Visual Studio is not giving me an unused variable warning. - If I make the type of the parameter testString a string or even an int, the code below will route to the catcher. If I make it some variation of a model or a Jobject, it will not. It gets as far as running

HttpResponseMessage response = await client.PostAsJsonAsync("api/activity", sendData);

then just returns to the web page.

Here's the code:

Models

public class testStringModel
{
        public string testString { get; set; }
}

public class apiResponse
{
    public string response { get; set; }
}

Home controller calling Api:

   public void TestApi()
    {
        Task myTask = testApiCall();
    }


    private async Task<string> testApiCall()
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:55144");

        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

        testStringModel data = new testStringModel { testString = "cheese" };
        string jsonData = JsonConvert.SerializeObject(data);
        var sendData = new StringContent(jsonData, Encoding.UTF8, "application/json");
        //var sendData = new Dictionary<string, string>
        //{
        //  {"testString", "cheese"}
        //};


        HttpResponseMessage response = await client.PostAsJsonAsync("api/activity", sendData);
        string responseBodyAsText = await response.Content.ReadAsStringAsync();

        dynamic stuff = JObject.Parse(responseBodyAsText);
        string finalResponse = stuff.response;


        return finalResponse;

        }
    }

The api:

namespace ApplicationActivity
{
public class ActivityController : ApiController
{
    [System.Web.Http.HttpPost]
    public HttpResponseMessage Catcher()
    {
        apiResponse apiResponseObject = new apiResponse();
        apiResponseObject.response = "You have somehow wound up in the catcher";

        string json = JsonConvert.SerializeObject(apiResponseObject);

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
        response.Content = new StringContent(json, Encoding.Unicode, "application/json");
        response.Headers.CacheControl = new CacheControlHeaderValue()
        {
            MaxAge = TimeSpan.FromMinutes(20)
        };
        return response;

    }

    [System.Web.Http.HttpPost]
    public HttpResponseMessage TestApiMethod(string testString)
    {
        apiResponse apiResponseObject = new apiResponse();
        apiResponseObject.response = "OK from test";

        string json = JsonConvert.SerializeObject(apiResponseObject);

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
        response.Content = new StringContent(json, Encoding.Unicode, "application/json");
        response.Headers.CacheControl = new CacheControlHeaderValue()
        {
            MaxAge = TimeSpan.FromMinutes(20)
        };
        return response;

    }     
}

}

Please will you tell me what I'm doing wrong with my code, how to fix it and what is happening when the code doesn't get to the catcher?

Thanks.

Phil_BHN
  • 1
  • 3
  • Your code looks correct. I don't know if it makes any difference, but my code has: `client.DefaultRequestHeaders.Add("User-Agent", "My app name");` after the creation of HttpClient. – Rick Wolff Jun 28 '18 at 14:24
  • Have you tried: `HttpResponseMessage response = await client.PostAsJsonAsync("api/activity", data);` and `public HttpResponseMessage TestApiMethod(testStringModel testString)`? – Rick Wolff Jun 28 '18 at 14:33
  • @Rick, thanks for taking a look. When I try your suggestion, it displays the empty page. Breakpoints in both controllers are not hit. – Phil_BHN Jun 28 '18 at 14:57
  • Another user commented here and deleted the comment. He/she was pointing to this question: https://stackoverflow.com/questions/9569270/custom-method-names-in-asp-net-web-api Maybe it's somehow related... – Rick Wolff Jun 28 '18 at 15:07

1 Answers1

0

It turns out that I was using an older version of visual studio and as a result the whole thing got really confused with whether is was running .net core or not. Upgrading to the latest and making sure the latest .net core is installed solved most of my troubles

Phil_BHN
  • 1
  • 3