【问题标题】:web api core 2 post internal errorweb api core 2发布内部错误
【发布时间】:2018-03-07 03:58:16
【问题描述】:

嗨,我在 Windows 7 上使用 vs 2017 我正在关注 this 关于复数视线的教程:.net core 2 web api。 GET 方法工作正常。有内部服务器错误 500。我感谢任何人检测到此错误的来源。我正在使用 Postman / Fiddler 两者都给出相同的 500 错误:

 [HttpPost("{cityid}/pointofinterest", Name = "GetPointOfInterest")]
    public IActionResult CreatePointofInterest(int cityid, [FromBody]
    PointofInterestForCreationtDto inputPointofinterest)
    {

        if (inputPointofinterest == null)
        {
            return BadRequest();
        }
        var theCity = (CitiesRepo.Current.Cities.FirstOrDefault(x => x.Id == cityid));
        if (theCity == null)
        {
            return NotFound();
        }
        //get new id
      var maxid=  CitiesRepo.Current.Cities.SelectMany(
                         c => c.PointsOfInterest).Max(p => p.Id);

        var postedPointofInterest = new PointOfInterestDto()
        {
            Id = ++maxid,
            Name = inputPointofinterest.Name,
            Description = inputPointofinterest.Description
        };
        theCity.PointsOfInterest.Add(postedPointofInterest);

        return CreatedAtRoute("GetPointOfInterest",
            new { CityId= cityid, id = postedPointofInterest.Id, postedPointofInterest });

    }

【问题讨论】:

  • 提示:使用预览标签来实际读取错误。
  • 感谢这是错误:处理请求时发生未处理的异常。 AmbiguousActionException:匹配多个操作。以下操作匹配路线数据并满足所有约束: CitiesInfo.Api.Controllers.CitiesController.CreatePointofInterest (CitiesInfo.Api) CitiesInfo.Api.Controllers.PointofInterestController.CreatePointofInterest (CitiesInfo.Api) M
  • 你是对的:我得到错误,有另一个具有相同路由和签名的方法

标签: asp.net-web-api asp.net-core-2.0


【解决方案1】:

@kogibail 500 Internal Server 错误通常在您的代码块中存在未处理的异常(如空引用异常)时引发。

在您的代码中,几乎不需要进行空值检查。例如,如果 CitiesRepo.Current.Cities 可以为 null,则可以检查它们是否为 null,或者可以验证为

var theCity = CitiesRepo.Current?.Cities?.FirstOrDefault(x => x.Id == cityid);

包含一个 try catch 块并返回异常消息以确定根本原因。修复异常后,删除 try catch。

【讨论】:

  • 虽然这是一个合理的建议,但完全正确地指出如果添加了try..catch,则应该删除它,最好学习如何使用调试器并避免编写额外的代码来确定导致错误。
【解决方案2】:
HttpPost("{cityid}/pointofinterest", Name = "GetPointOfInterest")]
    public IActionResult CreatePointofInterest(int cityid, [FromBody]
    PointofInterestForCreationtDto inputPointofinterest)
    {

try
{

        if (inputPointofinterest == null)
        {
            return BadRequest();
        }
        var theCity = (CitiesRepo.Current.Cities.FirstOrDefault(x => x.Id == cityid));
        if (theCity == null)
        {
            return NotFound();
        }
        //get new id
      var maxid=  CitiesRepo.Current.Cities.SelectMany(
                         c => c.PointsOfInterest).Max(p => p.Id);

        var postedPointofInterest = new PointOfInterestDto()
        {
            Id = ++maxid,
            Name = inputPointofinterest.Name,
            Description = inputPointofinterest.Description
        };
        theCity.PointsOfInterest.Add(postedPointofInterest);

        return CreatedAtRoute("GetPointOfInterest",
            new { CityId= cityid, id = postedPointofInterest.Id, postedPointofInterest });
}
catch(Exception e)
{
return { Error = e.Message }
}    }

【讨论】:

  • 我的错:我在具有相同路由类型的第二个控制器中使用了相同的方法....我删除了它,它现在可以工作了
猜你喜欢
  • 2017-08-05
  • 2018-08-03
  • 1970-01-01
  • 2017-03-07
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
  • 2020-07-10
相关资源
最近更新 更多