405方法不允许当在Postman中调用带有body参数的Put方法时

本文关键字:方法 body 参数 Put 调用 不允许 Postman | 更新日期: 2023-09-27 17:54:51

我试图通过Postman调用Put方法,但总是得到错误:
"405 method Not Allow"answers"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"answers"En"是我代码中的前两个参数。我还指定了"text"参数值在邮差请求体与x-www-form-urlencoded类型:key = text, value = Test!这个put方法应该将temp.Content的值更新为"Test!"。然而,它总是因为我上面提到的错误而失败。

我错过什么了吗?

405方法不允许当在Postman中调用带有body参数的Put方法时

对web api执行PUT请求时的405错误是一个众所周知的话题。你可以在这个或这个SO问题中找到很多解决方案。

对于您的控制器设计:

PUT被设计成有一个主体,就像POST一样,在你的例子中您应该在body中发送所有参数。

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

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
};

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

data: JSON.stringify(data),

然后到达PUT控制器"http://localhost:XXXX/multilanguage/resources/putresource"。

检查您发布数据的URL,在我的情况下,URL不正确,因为我得到了这些错误,还验证在Body中您应该选择raw并将文本更改为JSON,如果您正在传递JSON作为数据