【问题标题】:Error "405 Method Not Allow" When Calling Put method in Postman with body parameter在 Postman 中使用 body 参数调用 Put 方法时出现错误“405 Method Not Allow”
【发布时间】:2016-11-07 12:14:48
【问题描述】:

我试图通过 Postman 调用 Put 方法,但总是出现错误:
“405 Method Not Allow”和“Message”:“请求的资源不支持 http 方法 'PUT'。”

我正在使用 DocumentDB 和 C#。这是我的代码:

[Route("multilanguage/Resources/{id}/{Language}")]
[HttpPut]
public async Task<IHttpActionResult> UpdateResource(string Id, string Language, string text)
{
    client = new DocumentClient(new Uri(EndPoint), AuthKey);
    var collectionLink = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);

    var query = new SqlQuerySpec("SELECT * FROM MultiLanguage as m where m.id = @pmId", 
    new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@pmId", Value = Id } }));

    Document doc = client.CreateDocumentQuery<Document>(
            collectionLink, query).AsEnumerable().FirstOrDefault();

    List<Models.Translations> d = doc.GetPropertyValue<List<Models.Translations>>("Translations");
    Models.Translations temp = d.Find(p => p.Language == Language);

    temp.Content = text;
    temp.LastModified = DateTimeOffset.Now;
    temp.ModifiedBy = "admin";
    doc.SetPropertyValue("Translations", d);

    Document updated = await client.ReplaceDocumentAsync(doc);

    return Ok();
}

当我通过 Postman 调用 Put 方法时,我调用“http://localhost:XXXX/multilanguage/resources/2/En”。 “2”和“En”是我代码中的前两个参数。而且我还在 Postman 请求正文中使用 x-www-form-urlencoded 类型指定了“text”参数值:key = text, value = Test!这个 put 方法假设将 temp.Content 值更新为“Test!”。但是,它总是因我上面提到的错误而失败。

我错过了什么吗?

【问题讨论】:

    标签: c# http asp.net-web-api2 postman


    【解决方案1】:

    对 web api 执行 PUT 请求时出现 405 错误是众所周知的话题。您可以在this 或this SO 问题中找到许多解决方案。

    对于控制器的设计:

    PUT 被设计为有一个主体,就像 POST 一样,在你的情况下 您应该改为发送正文中的所有参数。

    您应该创建一个包含要发送到服务器的对象的类:

    public class resourceClass
    {
        public string Id { get; set; }
        public string Language { get; set; }
        public string text { get; set; }
    }
    

    然后指定不带属性路由的路由,从请求体中获取对象

    [Route("multilanguage/Resources/PutResource")]
    [HttpPut]
    public async Task<IHttpActionResult> UpdateResource([FromBody] resourceClass obj)
    {
        client = new DocumentClient(new Uri(EndPoint), AuthKey);
        var collectionLink = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
    
        var query = new SqlQuerySpec("SELECT * FROM MultiLanguage as m where m.id = @pmId", 
        new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@pmId", Value = Id } }));
    
        Document doc = client.CreateDocumentQuery<Document>(
                collectionLink, query).AsEnumerable().FirstOrDefault();
    
        List<Models.Translations> d = doc.GetPropertyValue<List<Models.Translations>>("Translations");
        Models.Translations temp = d.Find(p => p.Language == Language);
    
        temp.Content = text;
        temp.LastModified = DateTimeOffset.Now;
        temp.ModifiedBy = "admin";
        doc.SetPropertyValue("Translations", d);
    
        Document updated = await client.ReplaceDocumentAsync(doc);
    
        return Ok();
    }
    

    您可以从客户端向 Content-Type application/json 的 PUT 请求添加一个对象,如下所示

    var data = {
        Id: clientId,
        Language: clientLanguage,
        text: clientText
    };
    

    将json添加到http请求时不要忘记将其字符串化

    data: JSON.stringify(data),
    

    然后将通过“http://localhost:XXXX/multilanguage/resources/putresource”访问 PUT 控制器。

    【讨论】:

    • 太棒了!祝你有美好的一天
    猜你喜欢
    • 2017-06-14
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 2013-10-10
    • 2018-06-29
    相关资源
    最近更新 更多