C# Web Api响应
在浏览器中测试API时,只调用Get方法 当我浏览 我从数据库中得到正确的值。 这里GetStudentById是[HttpGet]下的[ActionName] 但是当我浏览的时候 I get“请求的资源不支持http方法'get' (为什么要重定向以获取)? 此处StudentDeleteById是[HttpDelete]下的[ActionName] 我已经在Postman中测试了我的API,每个请求都成功执行(即Get、Put、Post、Delete) 下面是我的WebApiConfig.csC# Web Api响应,c#,api,C#,Api,在浏览器中测试API时,只调用Get方法 当我浏览 我从数据库中得到正确的值。 这里GetStudentById是[HttpGet]下的[ActionName] 但是当我浏览的时候 I get“请求的资源不支持http方法'get' (为什么要重定向以获取)? 此处StudentDeleteById是[HttpDelete]下的[ActionName] 我已经在Postman中测试了我的API,每个请求都成功执行(即Get、Put、Post、Delete) 下面是我的WebApiConfig.c
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
当您在地址栏中键入URL时,浏览器仅发送GET请求。 为了发送删除请求,可以使用curl命令行util
curl -X "DELETE" http://localhost:xxxxx/api/student/StudentDeleteById/38
或者使用jQuery创建发送请求的小页面:
function sendDelete() {
$.ajax(
{
url: 'http://localhost/api/student/StudentDeleteById/38',
method: 'DELETE'
}).done(function () {
alert('done');
});
}因为如果在浏览器中执行该方法,实际上执行的是get请求,而不是delete请求,但您的操作标记为httpdelete,这就是为什么会忽略它,并且会出现错误