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



我尝试在浏览器中查看我的 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;
        }
    }

Testing方法顶部添加[HttpGet]

[HttpGet]
public HttpResponseMessage Testing()

或在方法名称处添加Get

public HttpResponseMessage GetTesting()

然后它应该可以通过 http://localhost:2797/api/Country/Testing

相关内容

  • 没有找到相关文章

最新更新