使用 body 参数在 Postman 中调用 Put 方法时出现错误“405 Method Not Allow”

Error "405 Method Not Allow" When Calling Put method in Postman with body parameter

我试图通过 Postman 调用 Put 方法,但总是出现错误:
“405 方法不允许”和 "Message":"The requested resource does not support http method '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 请求 Body 中指定 "text" 参数值 with x-www-form-urlencoded type: key = text, value = Test!此 put 方法假设将 temp.Content 值更新为 "Test!"。但是,它总是因我上面提到的错误而失败。

我在这里错过了什么吗?

向网络执行 PUT 请求时的 405 错误 api 是一个众所周知的话题。您可以在 this or this SO 问题中找到许多解决方案。

以及您的控制器设计:

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

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

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

然后指定没有属性routing的路由,从请求体中获取对象

[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 控制器。