【发布时间】:2016-04-09 22:44:26
【问题描述】:
我有一对可以正常工作的 GET 和 POST Web API 方法。我有另一对似乎以完全相同的方式设置,但 GET 方法拒绝接受呼叫。所有这样做的尝试都以消息结束,“请求的资源不支持 http 方法'GET'。”
然而(再次)这个 GET 方法的设置和装饰与工作方法相同。
这是工作的(未标记的“GET”(第一种方法)和标记的“POST”在调用时都成功输入):
[RoutePrefix("api/pricecompliance")]
public class PriceComplianceController : ApiController
{
[Route("{unit}/{begindate}")]
public HttpResponseMessage Get(string unit, string begindate)
{
. . .
[Route("{unit}/{begindate}")]
[HttpPost]
public void Post(string unit, string begindate)
{
. . .
...这是看起来几乎相同的一对(当然名称不同,还有一个参数):
[RoutePrefix("api/produceusage")]
public class ProduceUsageController : ApiController
{
[Route("{unit}/{begindate}/{enddate}")]
public HttpResponseMessage Get(string unit, string beginRange, string
endRange)
{
. . .
[Route("{unit}/{begindate}/{enddate}")]
[HttpPost]
public void Post(string unit, string begindate, string enddate)
{
. . .
如前所述,使用不工作的第二块代码,当我尝试调用“GET”时,我得到:
请求的资源不支持 http 方法 'GET'。
我确实有这样设置的属性路由:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
为什么后一种情况不支持 GET?
当我尝试通过单击带有 URL 的链接来调用 GET 时,以及当我尝试使用“http://localhost:52194/api/produceusage/gramps/201312/201412”从 Postman 调用它时,我都明白了相同的“GET 不支持”爵士乐。
【问题讨论】:
-
您是否尝试将其添加到方法中,看看是否有帮助? [System.Web.Http.HttpGet]
-
我实际上一开始在工作的时候有这个,但必须从那里删除它才能让它工作。
-
你有关于非工作的“使用 System.Web.Mvc”的声明吗?
-
否;我必须使路由参数名称与方法参数名称完全匹配(请参阅我的自动回答)。
标签: c# asp.net-web-api http-get asp.net-web-api-routing http-method