Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .Net核心C“Rest API”;“不允许使用方法”;_C#_Rest_.net Core - Fatal编程技术网

C# .Net核心C“Rest API”;“不允许使用方法”;

C# .Net核心C“Rest API”;“不允许使用方法”;,c#,rest,.net-core,C#,Rest,.net Core,我有一个.Net核心C#API,看起来像这样: [HttpPut] [Route("{primaryKey}")] public async Task<ActionResult<ResponseSingleX<ClassA>>> Update(Guid primaryKey, ClassA classA) { // Update database } [HttpPut] [路由(“{

我有一个.Net核心C#API,看起来像这样:

    [HttpPut]
    [Route("{primaryKey}")]
    public async Task<ActionResult<ResponseSingleX<ClassA>>> Update(Guid primaryKey, ClassA classA)
    {
        // Update database
    }
[HttpPut]
[路由(“{primaryKey}”)]
公共异步任务更新(Guid primaryKey,ClassA)
{
//更新数据库
}
我尝试使用以下代码进行更新:

// Create a ClassA object and put some data in it
...

//GetServiceUrl() == return "http://localhost:9003/api/Configurations/";
serviceUrl = _shared.GetServiceUrl() + classA.PrimaryKey.ToString();
string requestBody = System.Text.Json.JsonSerializer.Serialize(classA);
HttpContent requestContent = new StringContent(requestBody, Encoding.UTF8, "application/json");
HttpResponseMessage responseMessage = await client.PutAsync(serviceUrl, requestContent);
Stream responseStream = await responseMessage.Content.ReadAsStreamAsync();
responseMessage.EnsureSuccessStatusCode();
var updatedResponse = await System.Text.Json.JsonSerializer.DeserializeAsync<ResponseSingleX<ClassA>>(responseStream, _serializerOptions);
Assert.True(updatedResponse.Success);
var updatedClassA = updatedResponse.Element;
//创建一个ClassA对象并在其中放入一些数据
...
//GetServiceUrl()==返回“http://localhost:9003/api/Configurations/";
serviceUrl=_shared.GetServiceUrl()+classA.PrimaryKey.ToString();
string requestBody=System.Text.Json.JsonSerializer.Serialize(classA);
HttpContent-requestContent=newstringcontent(requestBody,Encoding.UTF8,“application/json”);
HttpResponseMessage responseMessage=await client.PutAsync(serviceUrl,requestContent);
Stream responseStream=await responseMessage.Content.ReadAsStreamAsync();
responseMessage.EnsureAccessStatusCode();
var updatedResponse=await System.Text.Json.JsonSerializer.DeserializeAsync(responseStream,_serializerOptions);
True(updatedResponse.Success);
var updatedClassA=updatedResponse.Element;
我遇到的问题是responseMessage==“方法不允许”。由于这是我第一次使用Rest(无论是哪种编程语言),我不明白为什么它不起作用

更新0:
我发现了我的错误。有关问题的原因,请参阅我的答案。谢谢你的帮助

尝试在ClassA之前添加[FromBody]属性:

[HttpPut]
[Route("{primaryKey}")]
public async Task<ActionResult<ResponseSingleX<ClassA>>> Update(Guid primaryKey, [FromBody] ClassA classA)
{
    // Update database
}
[HttpPut]
[路由(“{primaryKey}”)]
公共异步任务更新(Guid primaryKey,[FromBody]ClassA)
{
//更新数据库
}

我发现了我的错误。类本身有一个定义了附加参数的URL

[Route("...") // <-This is what I missed
[ApiController]
public class SomeController
{
    ...
}

[Route(“…”)//使用Fiddler代理。它将帮助您调试类似的事情。这可能是由于发送了错误类型的http请求。请检查您使用的是GET还是PUT。为了更好地控制发送请求,您应该使用邮递员或类似的工具。。。