天天看点

Message": "请求的资源不支持 http 方法“GET”

今天用postman测试后端api,总是报错,下面是问题解决方案。

一、测试方法

public apiresult get(int id)

{

apiresult result = new apiresult();

result.data = "我是get方法返回的数据";

result.success = true;

result.msg = "测试成功";

return result;

}

public class apiresult

public bool success { get; set; }

public string msg { get; set; }

public object data { get; set; }

二、问题和解决

0请求:http://localhost/projectmanagement/api/user/5

在网上查了下请求要这么写:http://localhost/projectmanagement/api/user/?id=5

1 提示没有权限

去路由里面一看,前辈重新了校验规则,不知道。为了测试方法我注释的校验规则。

public static void register(httpconfiguration config)

// web api 配置和服务

// web api 路由

config.maphttpattributeroutes();

config.routes.maphttproute(

name: "defaultapi",

routetemplate: "api/{controller}/{id}",

defaults: new { id = routeparameter.optional }

);

// config.filters.add(new appauthorizeattribute());

2   "message": "请求的资源不支持 http 方法“get”。"

请求 

解决:在方法代码上一行 添加 [httpget]

3 又报 "message": "请求的资源不支持 http 方法“get”。"

解决:同时在多个方法代码 添加 [httpget],必须取别名 ,例如 [route("get")]  

请求也要加别名:http://localhost/projectmanagement/api/user/get/?id=5

http://localhost/projectmanagement/api/user/first/?id=5

然后还是报错。

解决:使用别名时,必须在控制器类上面加前缀    [routeprefix("api/user")]

三、完整测试代码

  

Message": "请求的资源不支持 http 方法“GET”

树立目标,保持活力,gogogo!