Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Web Api响应_C#_Api - Fatal编程技术网

C# Web Api响应

C# 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

在浏览器中测试API时,只调用Get方法

当我浏览 我从数据库中得到正确的值。 这里GetStudentById是[HttpGet]下的[ActionName]

但是当我浏览的时候 I get“请求的资源不支持http方法'get' (为什么要重定向以获取)? 此处StudentDeleteById是[HttpDelete]下的[ActionName]

我已经在Postman中测试了我的API,每个请求都成功执行(即Get、Put、Post、Delete)

下面是我的WebApiConfig.cs

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,这就是为什么会忽略它,并且会出现错误