网络API - 405 - 请求的资源不支持HTTP方法“把' [英] Web API - 405 - The requested resource does not support http method 'PUT'

查看:3337
本文介绍了网络API - 405 - 请求的资源不支持HTTP方法“把'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web API项目,我无法使反对PUT /补丁的请求。

I have a Web API project and I am unable to enable "PUT/Patch" requests against it.

我从小提琴手得到的回应是:

The response I get from fiddler is:


HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache
Pragma: no-cache
Allow: GET,POST,DELETE
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'."}

根据上述回应,PUT的动词是不能接受的。不过,我无法找出相关的处理程序被配置在哪里。

Based on the above response, "PUT" verbs are not accepted. However, I'm unable to figure out where the related handler is configured.

类的放的方法声明如下:

The "Put" method of class is declared as follows:

[HttpPatch]
[HttpPut]
public HttpResponseMessage Put(Int32 aID, [FromBody] ImpressionModel impressionModel)
{
     bla, bla, bla, bla
}

我已经阅读并实施的更改在以下主题解释说:
- <一个href=\"http://stackoverflow.com/questions/9854602/asp-net-web-api-405-http-verb-used-to-access-this-page-is-not-allowed-how/23497295#23497295\">Asp.NET网络API - 405 - 用来访问本页面的HTTP动词是不允许的 - 如何设置处理器映射
- <一个href=\"http://www.asp.net/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications\">http://www.asp.net/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications

什么都没有做过,我仍然得到试图发出一个对我的Web API项目PUT命令时,405的回应。

Nothing has worked as I'm still getting a 405 response when trying to issue a "PUT" command against my Web API project.

我甚至在ApplicationsHost.config文件中注释掉所有的处理程序的。

I even commented out all of the "Handlers" in the ApplicationsHost.config file.

用VS2012 premium和IIS防爆preSS(我假设它是第8版)工作。我也试过VS开发服务器,但是这给了我同样的结果也。

Working with VS2012 Premium and IIS Express (I'm assuming it's version 8). I also tried the VS Dev Server but that gave me the same result also.

我的想法。任何帮助将是AP preciated。

I'm out of ideas. Any help would be appreciated.

谢谢李

推荐答案

您使用属性路由?

这神秘的错误是一个路由属性的问题。这是在您的WebAPIConfig启用为:

This mystic error was a route attributes issue. This is enabled in your WebAPIConfig as:

 config.MapHttpAttributeRoutes(); 

原来网页API控制器不能承载的基于动词的动作方式和传统的动作名称路由的混合物。 <一href=\"https://aspnetwebstack.$c$cplex.com/workitem/184\">https://aspnetwebstack.$c$cplex.com/workitem/184

简而言之:我需要标记所有我在我的[路线]属性API控制器动作,否则试图通过传统的路由找到它时动作是隐藏(405'd)。

API控制器:

[RoutePrefix("api/quotes")]
public class QuotesController : ApiController
{
    ...

    // POST api/Quote
    [ResponseType(typeof(Quote))]
    [Route]
    public IHttpActionResult PostQuote(Quote quote)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Quotes.Add(quote);
        db.SaveChanges();

        return CreatedAtRoute("", new { id = quote.QuoteId }, quote);
    }

注:我的路线是无名所以CreatedAtRoute()的名字只是一个空字符串

WebApiConfig.cs:

WebApiConfig.cs:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

        // Web API routes
        config.MapHttpAttributeRoutes();

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

    }
}

希望这有助于

这篇关于网络API - 405 - 请求的资源不支持HTTP方法“把'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆