2015-11-02 33 views
1

我试图调用此方法没有介质类型格式化发现

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

与URL http://localhost:46789/api/Trinity/GetDirectoryAndTask/9/AG33%2f34,但我得到一个“MediaTypeFormatter可从与媒体类型的内容读类型的对象‘KeyValuePair`2’ 'text/html'“异常。

+0

听起来像你有同样的问题,因为这家伙http://stackoverflow.com/questions/12512483/no-mediatypeformatter-is-available -to-读一个对象 - 的类型字符串从 - 康特 – Dietz

回答

1

因为在路由值使用/%2f的,我怀疑是在服务器端的主要问题应该是:

HTTP错误404.0 - 找不到
你正在寻找一直的资源删除,更名或暂时不可用。

,解决它,你可以更改路由到这一点:

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

要测试服务器端是确定的,粘贴浏览器的URL并得到结果。


要不是你客户的错误与您从API读取数据的方式。我使用此代码后,我改变了路线它读取数据:

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 
    } 
} 
相关问题