1

I'm trying to call this method

    [HttpGet]
    [Route("api/Trinity/GetDirectoryAndTask/{modelTemplateId}/{taskName}")]
    public KeyValuePair<string, string> GetDirectoryAndTask(int modelTemplateId, string taskName)

with the url http://localhost:46789/api/Trinity/GetDirectoryAndTask/9/AG33%2f34 but am getting a "MediaTypeFormatter is available to read an object of type 'KeyValuePair`2' from content with media type 'text/html'" exception.

reggaeguitar
  • 1,795
  • 1
  • 27
  • 48
  • Sounds like you are having the same problem as this guy http://stackoverflow.com/questions/12512483/no-mediatypeformatter-is-available-to-read-an-object-of-type-string-from-conte – Dietz Nov 03 '15 at 09:05

1 Answers1

1

because of using / or %2f in route values, I suspect that the main problem at server side should be:

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

And to solve it you can change routing to this:

api/Trinity/GetDirectoryAndTask/{modelTemplateId}/{*taskName}

To test if the server side is OK, paste the url in browser and get the result.


But for your client the error is related to the way you read data from that api. I use this code and it reads data after I changed the route:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(" http://localhost:46789/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = await client.GetAsync("api/Trinity/GetDirectoryAndTask/9/AG33%2f34");
    if (response.IsSuccessStatusCode)
    {
        var result = await response.Content.ReadAsAsync<KeyValuePair<string, string>>();
        //The result is a valid key/value pair
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398