2017-06-21 68 views
0

我試圖在瀏覽器中查看我的API控制器動作,我似乎無法找到此endpiont解析的URL。我想:我的簡單的API控制器動作似乎沒有工作

http://localhost:2797/api/Country/Testing  
http://localhost:2797/api/Country/GetTesting  

我得到這個錯誤:

<Error> 
<Message> 
The requested resource does not support http method 'GET'. 
</Message> 
</Error> 


public class CountryController : ApiController 
    { 
     public HttpResponseMessage Testing() 
     { 
      HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value"); 
      response.Content = new StringContent("hello", System.Text.Encoding.Unicode); 
      response.Headers.CacheControl = new CacheControlHeaderValue() 
      { 
       MaxAge = TimeSpan.FromMinutes(20) 
      }; 

      return response; 
     } 

    } 
+0

請向我們展示您在'WebApiConfig.Register'中的路由配置。如果你沒有改變標準路由,你的控制器的正確URI就是'api/{controller}',沒有'{action}'部分。 –

回答

0

Testing方法

[HttpGet] 
public HttpResponseMessage Testing() 

的頂部添加[HttpGet]或方法名稱添加Get

public HttpResponseMessage GetTesting()

然後它應該可以通過 http://localhost:2797/api/Country/Testing

+0

謝謝。當我這樣做時,Chrome會要求我下載一個內含「hello」文件的文件。這是爲什麼?標題問題? –

+0

這意味着現在你的方法正在工作,關於下載文本文件:你的內容是字符串內容'new StringContent'(' – RezaRahmati

+0

remove'response.Content = ..'' total – RezaRahmati