请求的资源不支持 HTTP 方法“GET" [英] The requested resource does not support HTTP method 'GET'

查看:38
本文介绍了请求的资源不支持 HTTP 方法“GET"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路由配置正确,我的方法有装饰标签.我仍然收到请求的资源不支持 HTTP 方法 'GET'"消息?

My route is correctly configured, and my methods have the decorated tag. I still get "The requested resource does not support HTTP method 'GET'" message?

[System.Web.Mvc.AcceptVerbs("GET", "POST")]
[System.Web.Mvc.HttpGet]
public string Auth(string username, string password)
{
  // Décoder les paramètres reçue.
  string decodedUsername = username.DecodeFromBase64();
  string decodedPassword = password.DecodeFromBase64();

  return "value";
}

这是我的路线:

config.Routes.MapHttpRoute(
    name: "AuthentificateRoute",
    routeTemplate: "api/game/authentificate;{username};{password}",
    defaults: new { controller = "Game",
                    action = "Auth", 
                    username = RouteParameter.Optional, 
                    password = RouteParameter.Optional },
    constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);

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

推荐答案

请在您的 WebAPI 操作中使用 System.Web.Http 命名空间中的属性:

Please use the attributes from the System.Web.Http namespace on your WebAPI actions:

    [System.Web.Http.AcceptVerbs("GET", "POST")]
    [System.Web.Http.HttpGet]
    public string Auth(string username, string password)
    {...}

它不起作用的原因是因为您使用了来自 MVC 命名空间 System.Web.Mvc 的属性.System.Web.Http 命名空间中的类用于 WebAPI.

The reason why it doesn't work is because you were using the attributes that are from the MVC namespace System.Web.Mvc. The classes in the System.Web.Http namespace are for WebAPI.

这篇关于请求的资源不支持 HTTP 方法“GET"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆