我的简单API控制器操作似乎不起作用

时间:2017-06-21 19:33:29

标签: c# asp.net-mvc asp.net-apicontroller

我尝试在浏览器中查看我的API控制器操作,但我似乎无法找到此endpiont解析的URL。我试过了:

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;
        }

    }

1 个答案:

答案 0 :(得分:0)

[HttpGet]方法

之上添加Testing
[HttpGet]
public HttpResponseMessage Testing()

或在方法名称

添加Get

public HttpResponseMessage GetTesting()

然后它应该可以访问 http://localhost:2797/api/Country/Testing