【问题标题】:ASP.NET Web API - The requested resource does not support http method 'GET'ASP.NET Web API - 请求的资源不支持 http 方法“GET”
【发布时间】:2020-09-19 00:19:00
【问题描述】:

我正在使用 Postman 对我的 Web API 进行 POST 调用。我收到错误消息:“请求的资源不支持 http 方法 'GET'。” 我没有打 GET 电话。如果我确实调用了我的 GET 方法之一,它可以正常工作并返回预期的结果。

我的控制器类:

[RoutePrefix("api/login")]
    public class LoginController : ApiController
    {
        ModelContext db = new ModelContext();

        [HttpPost]
        [Route("validate")]
        public HttpResponseMessage Validate([FromBody] LoginViewModel login)
        {
            try
            {

                    var message = Request.CreateResponse(HttpStatusCode.OK);
                    return message;
            }
            catch (Exception ex)
            {
                var message = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
                return message;
            }
        }        

    }

我在本地运行 Web API 并使用此 URL 调用:

http://localhost:44303/api/login/validate

此网址返回:

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

我在 WebApiConfig.cs 中的路由

config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

虽然此控制器消息返回 HttpResponseMessage,但我已经通过将响应更改为“字符串”并仅返回一个字符串值进行了测试,但我得到了同样的错误。 我已经阅读了很多 SO 帖子,但似乎没有一个可以解决我的问题。我完全无法解释这种行为。我很欣赏任何想法。

编辑 我已经在其他控制器中测试了 GET,它们按预期返回数据。

编辑上下文 2020 年 6 月 3 日 默认 ValuesController 中的此方法有效:

[Route("api/values")]
public IEnumerable<string> Get()
{
   return new string[] { "value1", "value2" };
}

SAME 控制器中的这两种方法不起作用:

// GET api/values/5
public string Get(int id)
{
  return "value";
}

// POST api/values
public void Post([FromBody]string value)
{
}

编辑 #2 2020 年 6 月 3 日 现在我所有的 api 方法都适用于默认的 ValuesController。我不知道为什么。 我在其他控制器中的自定义 POST 方法(例如上面的 Post 方法)仍然无法正常工作。这是我当前的 WebApiConfig.cs:

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "Api_Get",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional, action = "Get" },
                constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
            );

            config.Routes.MapHttpRoute(
                name: "Api_Post",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional, action = "Post" },
                constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
            );
        }

我没有任何我知道的中间件或特殊路由。任何异常处理都是简单的 try-catch。

我正在尝试使用属性路由与约定,但这似乎是一个问题。

【问题讨论】:

  • FromBody 需要有效负载。它在哪里?你在用 Postman 吗?
  • 我正在使用 Postman,只是将虚拟用户名和密码作为 json 传递
  • 您的解决方案中是否有任何中间件或任何类型的自定义可能会弄乱路由或更改请求标头?
  • 你确定你正确使用邮递员吗?您的 Action 确实是一个 POST 操作,并且您的 API 上不存在 Action GET
  • 为什么没有 if 的还有 else?我缺少什么?

标签: c# asp.net-web-api asp.net-web-api-routing


【解决方案1】:

你不需要通过 HttpMethods 创建路由表,因为你有[HttpPost] 属性。

将所有config.Routes.MapHttpRoute替换为

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

Web API 项目最好使用属性路由。它将减少出错的机会,因为在 RouteConfig 类中创建新的自定义路由时可能会出现任何错误。此外,您不必处理路由流程,即从最具体到最一般。这里都是你使用action方法的属性。

【讨论】:

  • 如果它修复了错误,请告诉我:“请求的资源不支持 http 方法 'GET'。”
  • 在大多数情况下,Get 方法都在工作,但我仍在试图弄清楚为什么 Posts 没有。今晚我将使用 ssl 和整体发布到一个测试站点,看看会发生什么。它也可能是CORS的问题吗?我认为我没有在这个项目中明确设置 CORS。
猜你喜欢
  • 2012-06-15
  • 2016-11-26
  • 2013-03-05
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
  • 2012-09-27
相关资源
最近更新 更多