繁体   English   中英

Web Api 响应

[英]Web Api Response

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

当我浏览http://localhost:xxxxx/api/student/GetStudentById/38时,我从数据库中获得了正确的值。 这里的GetStudentById 是[HttpGet] 下的[ActionName]。

但是当我浏览http://localhost:xxxxx/api/student/StudentDeleteById/38时,我得到“请求的资源不支持 http 方法 'GET'。” (为什么它被重定向到Get)? 这里的 StudentDeleteById 是 [HttpDelete] 下的 [ActionName]。

我已经在 Postman 中测试了我的 API,每个请求都成功执行(即获取、放置、发布、删除)

下面是我的 WebApiConfig.cs

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

当您在地址栏中键入 URL 时,浏览器仅发送 GET 请求。 为了发送删除请求,您可以使用 curl 命令行工具

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');
  });

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM