【问题标题】:WebApi "The requested resource does not support http method 'DELETE"WebApi“请求的资源不支持http方法'DELETE”
【发布时间】:2017-06-14 09:18:25
【问题描述】:

使用 WebApi angularjs 项目并尝试删除函数为 `

     [HttpDelete]
    public String DeleteCountry(string cntryId)
    {
        if (cntryId != null)
        {
            return repoCountry.DeleteCountry(Convert.ToInt32(cntryId));

        }
        else
        {
            return "0";

        }
    }

js函数是

  $http({
            method: "delete",
            url: '/api/Country/DeleteCountry/',
            dataType: "json",
            data: { cntryId: cntryId }
        }).then(function (response) {});

我在这里遇到异常

{"Message":"The requested resource does not support http method 'DELETE'."}

插入、更新和获取功能正常工作。提供解决方案以及为什么仅删除会发生这种情况

【问题讨论】:

  • 您可以尝试删除“数据”对象并将 cntryId 附加到 url 吗?我的猜测是 ASP.NET 路由正在尝试查找没有参数的 HttpDelete 操作(因为 url 中没有)。
  • 该参数没有标记为[FromBody],所以如果我没记错的话必须放在URL中。
  • @Gusman 没错,默认情况下,绑定会从 URL 查询参数中查找“简单”类型。

标签: c# angularjs asp.net-web-api asp.net-web-api-routing


【解决方案1】:

使用 Route 属性装饰您的方法(我看到这让我可以更好地控制 Web API 中的路由行为)并以这种格式将您的数据参数作为构造函数参数传递:[HttpDelete, Route("{cntryId}"):

[HttpDelete, Route("{cntryId}")]
public String DeleteCountry(string cntryId)
  {
    //....
  }

在角度控制器中,您可以这样做:

$http.delete('/api/Country/' + cntryId).then(function (response) {
            //if you're waiting some response
        })

【讨论】:

  • 如果您没有更改 webapiconfig,则不需要 {cntryId}
  • @BRAHIMKamel 您将需要 route 属性,除非您在 api 配置中将默认路由从 {id} 更改为 {cntryId}。否则,任何未命名为 'id' 的参数都将作为查询字符串路由。
【解决方案2】:

不是 webapi 问题更多的是您的查询格式。 消息说 does not support http method 'DELETE' 因为 webapi delete 方法需要一个 id 作为参数。并且路线具有以下格式routeTemplate: "api/{controller}/{id}", 要解决您的问题,请尝试使用 fiddler 拦截您的请求并确保您的删除请求以 '/api/Country/DeleteCountry/'+cntryId, 发送。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题并最终解决了。这是一个简单而愚蠢的解决方案。

    之前的代码

    public String DeleteCountry(string cntryId)
    

    更改代码

    public String DeleteCountry(int id)
    

    所以只使用“int id”而不是“cntryId”。我没有在方法名称前使用 [HttpDelete],但它有效。

    【讨论】:

    • 天啊。我浪费了三个小时。我偶然发现了这个并想,“不!,这不可能。”将我的变量类型从 int 更改为 string 并开始工作。没有我能想到的特别原因,但这就是解决它的原因。
    【解决方案4】:

    选项1:

    $http({
       method: "delete",
       url: '/api/Country/Country?cntryId=' + cntryId,
    }).then(function (response) {});
    

    选项2:

    public String DeleteDeleteCountry([FromBody]string cntryId)
    {
        if (cntryId != null)
        {
            return repoCountry.DeleteCountry(Convert.ToInt32(cntryId));
    
        }
        else
        {
            return "0";
    
        }
    }
    

    最佳选择:

    API

    [Route("{countryId}")]
    public IHttpActionResult Delete(int countryId)
    {
        try
        {
           repoCountry.DeleteCountry(countryId);
        }
        catch (RepoException ex)
        {
           if (ex == notfound)
              this.NotFound();
           if (ex == cantdelete)
              this.Confict();
           this.InternalServerError(ex.message);
        }
        return this.Ok();
    }
    

    Javascript

    $http({
       method: "delete",
       url: '/api/country/' + cntryId,
    }).then(function (response) {});
    

    【讨论】:

      【解决方案5】:

      假设你没有改变默认路由。如果您未能在 webAPI 中为操作声明 [HttpDelete] 属性,也会发生同样的错误。请尝试关注

      [HttpDelete]
      public IHttpActionResult Delete(int id)
      {
      }
      

      【讨论】:

        【解决方案6】:

        如果您尝试执行 PUTDELETE 并且您没有配置 API 以允许它,这通常会发生。

        这篇文章向您展示了如何解决这个 405 错误并使其正常工作。

        编辑Web API项目web.config文件中ExtensionlessUrlHandler-Integrated-4.0行的最简单方法

        <system.webServer>
            <handlers>
                <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
                <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
            </handlers>
        </system.webServer>
        

        【讨论】:

          猜你喜欢
          • 2017-06-13
          • 2015-10-08
          • 2017-10-17
          • 2016-11-18
          • 2018-03-21
          • 1970-01-01
          • 2018-06-24
          • 2013-03-19
          相关资源
          最近更新 更多