Web API 2成功完成,但返回服务器错误500

时间:2018-08-13 20:56:27

标签: asp.net-web-api2

我已经在c#中使用Web api 2创建了一个控制器。

我的控制器方法是一篇文章,并成功完成,返回201

as shown below

但是当客户端收到响应时,将返回500内部服务器错误。

有人可以告知为什么会这样吗?

我认为一旦控制器返回一个值,请求就完成了,显然不是这种情况。

    [HttpPost]
    [Route("PostProperty/{agentId}")]
    public async Task<IActionResult> PostProperty(int agentId, [FromBody]CreatePropertyRequestDto value)
    {
        string result = await _mediator.Send(new NewPropertyRequest() { NewProperty = value, AgentId = agentId });
        var c = CreatedAtRoute("getproperties", new { agentId, propertyIds = result }, result);
        return c;
    }

如果我在输出窗口中查看,可以看到抛出了500,但是我仍然不知道在哪里或为什么。请帮助

Application Insights Telemetry (unconfigured): {
    "name": "Microsoft.ApplicationInsights.Dev.Request",
    "time": "2018-08-13T20:51:39.9717971Z",
    "tags": {
        "ai.operation.name": "POST Properties/PostProperty [agentId]",
        "ai.cloud.roleInstance": "DESKTOP-DM70VVS",
        "ai.location.ip": "127.0.0.1",
        "ai.application.ver": "1.0.0.0",
        "ai.operation.id": "4d86444a-4e214be6ea456695",
        "ai.internal.nodeName": "DESKTOP-DM70VVS",
        "ai.internal.sdkVersion": "aspnet5c:2.1.1"
    },
    "data": {
        "baseType": "RequestData",
        "baseData": {
            "ver": 2,
            "id": "|4d86444a-4e214be6ea456695.",
            "name": "POST Properties/PostProperty [agentId]",
            "duration": "00:00:02.7187869",
            "success": false,
            "responseCode": "500",
            "url": "http://localhost:63103/api/properties/PostProperty/1",
            "properties": {
                "httpMethod": "POST",
                "AspNetCoreEnvironment": "Development",
                "DeveloperMode": "true"
            }
        }
    }
}

编辑:

如果我返回OK(result),则返回200。现在,我可以确认CreatedAtRoute()是元凶。我已在Startup.cs中启用app.UseDeveloperExceptionPage();,并向我显示了一条有用的消息

  

InvalidOperationException:没有路由与提供的值匹配。

正在阅读this线程,我可以看到我必须与要说的内容创建动作的参数相匹配。

更新后的操作

    [HttpPost]
    [Route("PostProperty/{agentId}")]
    public async Task<IActionResult> PostProperty(int agentId, [FromBody]CreatePropertyRequestDto value)
    {
        string result = await _mediator.Send(new NewPropertyRequest() { NewProperty = value, AgentId = agentId });
        return CreatedAtRoute($"GetProperty", new { agentId = agentId, propertyId = result });
    }

上面引用的操作

    [HttpGet]
    [Route("GetProperty/{agentId}/{propertyId}")]
    public async Task<IActionResult> GetProperty(int agentId, string propertyId)
    {
        if (agentId <= 0 || propertyId == null)
        {
            return BadRequest("agentId or propertyId was invalid. agentId must be a valid id and propertyId cannot be null");
        }

        IEnumerable<PropertyDto> properties = await GetPropertiesRequest(agentId, new List<string>(){ propertyId });
        PropertyDto property = properties.FirstOrDefault();
        if (property == null)
        {
            return NotFound();
        }
        return Ok(property);
    }

即使更新了我的帖子操作,我仍然可以得到

  

InvalidOperationException:没有路由与提供的值匹配。

以下还返回相同的异常

CreatedAtRoute(routeName: "GetProperty", routeValues: new { agentId = agentId, propertyId = result }, value: result);

1 个答案:

答案 0 :(得分:1)

我使用CreatedAtAction()解决了我的问题。在这种情况下,CreatedAtRoute更适合,因为它在同一控制器中。有关差异的更详细说明,请阅读this thread

CreatedAtAction($"GetProperty", new { agentId = agentId, propertyId = result }, result);