【发布时间】:2018-02-21 12:59:43
【问题描述】:
HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache
Pragma: no-cache
Allow: GET,POST
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcUHJvamVjdHNcZG90TmV0XFdlYkFQSVxBZFNlcnZpY2VcQWRTZXJ2aWNlXGFwaVxpbXByZXNzaW9uXDE1?=
X-Powered-By: ASP.NET
Date: Tue, 06 May 2014 14:10:35 GMT
Content-Length: 72
{"message":"The requested resource does not support http method 'PUT'."}
我想使用 POSTMAN 生成 PUT 和 DELETE 请求,但我收到了来自 POSTMAN 的以下消息。
即使我已经实现了 ASP.NET 网站给出的所有建议。
以下是 Web API c# 代码:
// PUT: api/Students/5
[HttpPut]
[ResponseType(typeof(void))]
public IHttpActionResult PutStudent(decimal Enrollment_no, Student student)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (Enrollment_no != student.Enrollment_no)
{
return BadRequest();
}
db.Entry(student).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!StudentExists(Enrollment_no))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
当我尝试对我的 Web API 项目发出“PUT”命令时,仍然收到 405 响应,因此没有任何效果。
【问题讨论】:
-
您必须使用任何其他动词而不是“PUT”,请检查您的代码。
-
您需要向我们展示您的 Web API 控制器(或至少您已定义的方法)
-
添加你的控制器和路由代码。
-
要使用 PUT,您的操作方法必须具有属性
[HttpPut]。 -
响应显示允许的 HTTP 动词:
Allow: GET,POST
标签: c# asp.net-web-api http-put