MVC 尝试执行 Get 请求

MVC trying to do a Get request

我正在尝试在 MVC 4.0 (WebServiceREST) 上执行 GET 请求,某些方法使用 POST 而其他方法使用 GET 但我无法使其工作

我用了 [System.Web.Mvc.AcceptVerbs(HttpVerbs.Get)] 但还是没用 "The requested resource does not support the HTTP 'GET' method"

我的控制器:

  public class RecuperarDatosAppController : ApiController
{


    #region RecuperarClasesColectivas
    [System.Web.Mvc.AcceptVerbs(HttpVerbs.Get)]
    [ResponseType(typeof(List<ActividadesColectivas>))]
    public IHttpActionResult RecuperarClasesColectivas(short idInstalacion, string secretKey = "NOSECRETKEY")
    {
        BsSecurity bSecurity = new BsSecurity(BsSecurity.Tipo.Publica);
        if (bSecurity.comprobar(secretKey))
        {
            BsActividadesColectivas bsActividades = new BsActividadesColectivas();

            return Ok(bsActividades.GetActividadesColectivas(idInstalacion));
        }
        return NotFound();
    }

    #endregion


}

我认为您需要将 "Get" 添加到您希望通过 GET 请求公开的方法。

所以尝试 GetRecuperarClasesColectivas 而不是 RecuperarClasesColectivas

你还是会用/api/RecuperarClasesColectivas/id来称呼它,例如,路由只需要添加"Get"部分。

尽管您声明这是 MVC 并如此标记它,但它实际上是 Web API,因为您是从 System.Web.Http 命名空间继承自 ApiController. So you should decorate the method with the [HttpGet] attribute。请注意,将其重命名为像 Yoink 所建议的那样在前面加上 Get 是没有必要的,尽管这是常见的约定。