asp.net-web-api2 - Web api 2 成功完成但返回服务器错误 500
问题描述
我在 c# 中使用 web api 2 创建了一个控制器。
我的控制器方法是一个帖子并成功完成,返回 201
但是当客户端收到响应时,会返回 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()
是罪魁祸首。我已app.UseDeveloperExceptionPage();
在 Startup.cs 中启用,并向我展示了一条有用的消息
InvalidOperationException:没有路由与提供的值匹配。
阅读此线程,我可以看到我必须匹配我所说的创建项目的操作的参数。
更新的帖子操作
[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);
解决方案
我通过使用解决了我的问题CreatedAtAction()
。在这种情况下,它更适合,CreatedAtRoute
因为它在同一个控制器中。有关差异的更详细说明,请阅读此线程
CreatedAtAction($"GetProperty", new { agentId = agentId, propertyId = result }, result);
推荐阅读
- macos - 在 macos 上调试 .NET - 缺少 libsosplugin.so
- android - 未调用活动
- c# - Activator 未能创建对象实例,抛出 MissingMethodException
- variables - 常量变量重新定义?
- c - C链表 - 指针的值改变
- javascript - 如何解决:被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头
- node.js - 何时使用 Mongoose 建立索引
- reactjs - 反应钩子如何确定它们的组件?
- mysql - 如何从 MySql 中未规范化的表格单元格中解析/提取数据?
- python - 仅下载某些 MNIST 数字