405方法不允许使用web api

Xar*_*das 82 c# webdav asp.net-web-api

这个错误非常普遍,我尝试了所有的解决方案,但没有解决.我在控制面板中禁用了WebDAV发布,并将其添加到我的Web配置文件中:

  <handlers>
  <remove name="WebDAV"/>
  </handlers>
  <modules runAllManagedModulesForAllRequests="true">
  <remove name="WebDAVModule"/>
  </modules>
Run Code Online (Sandbox Code Playgroud)

错误仍然存​​在.这是控制器:

   static readonly IProductRepository repository = new ProductRepository();

    public Product Put(Product p)
    {
        return repository.Add(p);
    }
Run Code Online (Sandbox Code Playgroud)

方法实施:

 public Product Add(Product item)
    {
        if (item == null)
        {
            throw new ArgumentNullException("item");
        }
        item.Id = _nextId++;
        products.Add(item);
        return item;
    }
Run Code Online (Sandbox Code Playgroud)

这就是引发异常的地方:

client.BaseAddress = new Uri("http://localhost:5106/");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));      
var response = await client.PostAsJsonAsync("api/products", product);//405 exception
Run Code Online (Sandbox Code Playgroud)

有什么建议?

小智 58

我有同样的例外.我的问题是我用过:

using System.Web.Mvc; // Wrong namespace for HttpGet attribute !!!!!!!!!
[HttpGet]
public string Blah()
{
    return "blah";
}
Run Code Online (Sandbox Code Playgroud)

应该

using System.Web.Http; // Correct namespace for HttpGet attribute !!!!!!!!!
[HttpGet]
public string Blah()
{
    return "blah";
}
Run Code Online (Sandbox Code Playgroud)


Dar*_*rov 55

您正在从客户端发帖:

await client.PostAsJsonAsync("api/products", product);
Run Code Online (Sandbox Code Playgroud)

不是PUTing.

您的Web API方法仅接受PUT请求.

所以:

await client.PutAsJsonAsync("api/products", product);
Run Code Online (Sandbox Code Playgroud)


use*_*006 17

我试了很多东西让DELETE方法工作(我得到405方法不允许web api),最后我将[Route("api/scan/{id}")]添加 到我的控制器并且工作正常.希望这篇文章有所帮助.

     // DELETE api/Scan/5
    [Route("api/scan/{id}")]
    [ResponseType(typeof(Scan))]
    public IHttpActionResult DeleteScan(int id)
    {
        Scan scan = db.Scans.Find(id);
        if (scan == null)
        {
            return NotFound();
        }

        db.Scans.Remove(scan);
        db.SaveChanges();

        return Ok(scan);
    }
Run Code Online (Sandbox Code Playgroud)


Nex*_*xas 12

我的问题原来是WebAPI中的属性路由.我创建了一个自定义路由,它将其视为GET而不是WebAPI,发现它是一个POST

    [Route("")]
    [HttpPost] //I added this attribute explicitly, and it worked
    public void Post(ProductModel data)
    {
        ...
    }
Run Code Online (Sandbox Code Playgroud)

我知道它必须是愚蠢的(消耗你整天)


Nat*_*ugg 6

Chrome通常会OPTIONS在发布信息之前尝试拨打电话。这样做是为了确保CORS标头正确无误。如果未OPTIONS在API控制器中处理调用,则可能会出现问题。

public void Options() { }
Run Code Online (Sandbox Code Playgroud)


Mon*_*ode 6

我参加这个聚会迟到了,但由于上述任何方法在大多数情况下都不可行或有效,以下是我最终解决此问题的方法。

在托管站点/服务的服务器上,需要一个功能!HTTP 激活!!!

服务器管理器 > 管理 > 添加角色和功能 > 下一步 下一步 直到进入功能 > 在 .NET(每个版本)下勾选 HTTP 激活。另请注意,>net > WCF 服务下隐藏着一个。

这立即就起作用了!那让我的大脑融化了


Tom*_*kel 5

如果你的方法需要一个参数但你没有传递它,你也可能会收到 405 错误。

这不起作用(405错误)

HTML 视图/Javascript

$.ajax({
         url: '/api/News',
         //.....
Run Code Online (Sandbox Code Playgroud)

网络 API:

public HttpResponseMessage GetNews(int id)
Run Code Online (Sandbox Code Playgroud)

因此,如果方法签名与上面类似,那么您必须执行以下操作:

HTML 视图/Javascript

$.ajax({
         url: '/api/News/5',
         //.....
Run Code Online (Sandbox Code Playgroud)


Fra*_*ood 5

当服务器在https上时尝试连接到http时,也会发生此错误。

这有点令人困惑,因为我的get请求还可以,问题只存在于post-requests上。


net*_*otz 5

老问题,但没有一个答案对我有用。

本文通过添加以下行解决了我的问题web.config

<system.webServer>
  <modules runAllManagedModulesForAllRequests="false">
    <remove name="WebDAVModule" />
  </modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)