错误“405方法不允许”在邮递员中调用Put方法时使用body参数

问题描述:

我试图通过邮递员调用Put方法并始终出错:
“405方法不允许”和“消息”:“请求的资源不支持http方法'PUT'。“错误“405方法不允许”在邮递员中调用Put方法时使用body参数

我正在使用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(); 
} 

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

我在这里错过了什么吗?

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

并为您控制器的设计:

PUT被设计为有身体,就像邮政与你的情况 你应该在身体发出的所有参数来代替。

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

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应用/ json的PUT请求中,像这样

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

不要忘了将它添加到HTTP请求

data: JSON.stringify(data), 

的PUT控制器,然后在“http://localhost:XXXX/multilanguage/resources/putresource”到达时,该字符串化JSON。

+0

谢谢!这样可行! –

+0

太棒了!祝你今天愉快 –