WEB API错误-404 - “消息”:“请求的资源不支持http方法'PUT'。”

时间:2018-02-21 12:59:43

标签: c# asp.net-web-api http-put

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响应,没有任何效果。

2 个答案:

答案 0 :(得分:0)

你的后端api只允许GET和POST请求(参见响应头允许),因此生成PUT / DELETE请求API应该添加对它的支持。

答案 1 :(得分:0)

您可以通过修改ASP.NET Web API的web.config来配置它。尝试找到以下行:
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

将其替换为:
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

有关详细信息,请参阅here