错误“405方法不允许”使用body参数在Postman中调用Put方法时 [英] Error "405 Method Not Allow" When Calling Put method in Postman with body parameter
问题描述
我试图通过Postman调用Put方法并且总是收到错误:405 Method Not Allow和Message:请求的资源不支持http方法'PUT'。
I was trying to call the Put method through Postman and always getting error:
"405 Method Not Allow" and "Message": "The requested resource does not support http method 'PUT'."
我正在使用DocumentDB和C#。这是我的代码:
I'm using DocumentDB and C#. Here is my code:
[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是我代码中的前两个参数。我还使用x-www-form-urlencoded类型在Postman请求Body中指定text参数值:key = text,value = Test!这个put方法假设将temp.Content值更新为Test!。但是,它始终因上面提到的错误而失败。
我在这里错过了什么吗?
When I call the Put method throught Postman, I call "http://localhost:XXXX/multilanguage/resources/2/En". "2" and "En" are the first two parameters in my code. And I also specify the "text" parameter value in the Postman request Body with x-www-form-urlencoded type: key = text, value = Test! This put method suppose to update the temp.Content value to "Test!". However, it always failed with the error I mentioned above.
Did I miss anything here?
推荐答案
执行PUT时出现405错误对web api的请求是一个众所周知的主题。您可以在此<找到许多解决方案/ a>或这个 SO题。
The 405 error when performing a PUT request to web api is a well known topic. You can find many solutions in this or this SO question.
对于控制器的设计:
PUT设计有一个正文,就像POST一样在你的情况下
你应该发送身体中的所有参数。
PUT are designed to have a body, just like POST and in your case you should send all parameters in the body instead.
您应该创建一个包含要发送到服务器的对象的类:
You should create a class which contains the objects you want to send to the server:
public class resourceClass
{
public string Id { get; set; }
public string Language { get; set; }
public string text { get; set; }
}
然后指定没有属性路由的路由并从请求中获取对象正文
Then specify the route without the attribute routing and get the object from the request body
[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的PUT请求中application / json喜欢这个
From the client you could add an object to the PUT request of Content-Type application/json like this
var data = {
Id: clientId,
Language: clientLanguage,
text: clientText
};
在将json添加到http请求时,不要忘记对json进行字符串化
Don't forget to stringify the json when adding it to the http request
data: JSON.stringify(data),
然后将通过 http:// localhost:XXXX / multilanguage访问PUT控制器/资源/ putresource 。
这篇关于错误“405方法不允许”使用body参数在Postman中调用Put方法时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!