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")]
三、完整测试代码
using ProjectManagement.BLL; using ProjectManagement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace ProjectManagement.Controllers { [RoutePrefix("api/User")] public class UserController : ApiController { [HttpGet] [Route("First")] public ApiResult First(int id) { ApiResult result = new ApiResult(); result.data = "我是First方法返回的数据"; result.success = true; result.msg = "测试成功"; return result; } [HttpGet] [Route("Get")] public ApiResult Get(int id) { ApiResult result = new ApiResult(); result.data = "我是Get方法返回的数据"; result.success = true; result.msg = "测试成功"; return result; } } }
- 屏蔽WebApi中 请求资源不支持 http 方法"GET"的提示信息
- 请求的资源不支持 http 方法“GET”。
- 请求的资源不支持 http 方法“GET” 解决
- http get 请求地址传中文或值带特殊符号解决方法 URLEncoder.encode(str,"编码") new URLDecoder().decode(str,"编码")
- HTTP 协议请求方法GET 与POST 的区别?
- HTTP请求方法:GET和POST区别
- ios中http的两种请求方法:get与post
- HTTP请求协议 get post方法 获取数据解析
- PHP使用file_get_contents或curl请求https的域名内容为空或Http 505错误的问题排查方法
- HTTP请求方法之header(原来不光只有post和get啊~~)
- Http方法:Get请求与Post请求的区别
- HTTP协议语意 客户端如何请求服务器资源,HTTP方法列表(读RESTful webApi笔记)
- 发送http请求get方法
- Http方法:Get请求与Post请求的区别
- java http请求的get和post方法工具类
- java实现:http协议get和post方法的url参数请求响应及交互
- HTTP 协议请求方法GET 与POST 的区别?
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误
- 两种 HTTP 请求方法:GET 和 POST
- Android HttpClient GET或者POST请求基本使用方法