AspNetCore WebApi QueryString 参数已处理,但浏览器和邮递员说 "Server not found"

AspNetCore WebApi QueryString parameter is processed but browser & postman say "Server not found"

UPDATE:对于具有相同 error/behaviour 的人,请参阅答案 - 我自己的愚蠢错误,但错误消息 - 好吧 实际上没有给出真正的错误消息..所以可以说,行为很奇怪..至少,它已解决现在


原question/problem:

我正在学习使用 WebAPI 构建 REST-ful 服务,您可以在以下位置下载代码:https://github.com/pluralsight-courses/Implementing-and-Securing-an-API-with-ASP.NET-Core/tree/master/Module02_ReadingData *~ 克隆时请参阅下面的 注意

当我使用查询字符串 localhost:8088/api/camps/1?includeSpeakers=true 调用下面代码的 GET 时,参数的 return 出现问题 - 邮递员和浏览器都返回给我 "Server Not Found",并且浏览器甚至重定向到 http://www.localhost.com:8088/api/camps/1?includeSpeakers=true... 对我来说奇怪的行为是其他请求,例如 localhost:8088/api/camps/1localhost:8088/api/camps 工作正常。

如果我调试并单步执行,我会发现它运行良好并获取 QueryString,对数据库执行它,甚至达到 return Ok(camp); 行...但我什么也没得到 returned?

Fiddler:

Request Count: 1 Bytes Sent: 408 (headers:408; body:0) Bytes Received: 717 (headers:205; body:512)

ACTUAL PERFORMANCE

NOTE: This request was retried after a Receive operation failed.

ClientConnected: 14:20:36.281 ClientBeginRequest: 14:20:44.067 GotRequestHeaders: 14:20:44.067 ClientDoneRequest: 14:20:44.067 Determine Gateway: 0ms DNS Lookup: 0ms TCP/IP Connect: 0ms HTTPS Handshake: 0ms ServerConnected: 14:20:44.294 FiddlerBeginRequest: 14:20:44.294 ServerGotRequest: 14:20:44.294 ServerBeginResponse: 00:00:00.000 GotResponseHeaders: 00:00:00.000 ServerDoneResponse: 14:20:44.537 ClientBeginResponse: 14:20:44.537 ClientDoneResponse: 14:20:44.538

Overall Elapsed: 0:00:00.470

RESPONSE BYTES (by Content-Type) -------------- text/html: 512 ~headers~: 205

环境:

运行 Windows 10,Visual Studio 2017,IIS Express,本地主机端口 8088,未配置 SSL。

控制器:

using Microsoft.AspNetCore.Mvc;
using MyCodeCamp.Data;

namespace MyCodeCamp.Controllers {     
    [Route("api/[controller]")]     
public class CampsController : Controller {
        private readonly ICampRepository _campRepository;

        public CampsController(ICampRepository campRepository) {
            _campRepository = campRepository;
        }

        [HttpGet]
        public IActionResult Get() {
            try {
                var camps = _campRepository.GetAllCamps();
                return Ok(camps);
            } catch { }

            return BadRequest();
        }

        [HttpGet("{id}")]
        public IActionResult Get(int id, bool includeSpeakers = false) {
            try {
                var camp = includeSpeakers
                    ? _campRepository.GetCampWithSpeakers(id)
                    : _campRepository.GetCamp(id);
                if (camp == null) {
                    return NotFound($"Camp with id '{id}' was not found.");
                }
                return Ok(camp);
            } catch { }

            return BadRequest();
        }
    } 
}

~注意https://github.com/pluralsight-courses/Implementing-and-Securing-an-API-with-ASP.NET-Core/tree/master/Module02_ReadingData 克隆时首先执行 dotnet restore 并在 MyCodeCamp.Data 文件夹中确保执行 dotnet ef database update 初始化数据库,否则你将无法 运行 如果你想要的话。

当您发出请求时,请务必在 localhost 之前包含 http:// 否则 fiddler 将尝试执行 DNS 请求(这就是 www.localhost.com 重定向的原因)。如果这不起作用,请尝试使用 Postman 而不是 Fiddler。我觉得它更容易。

好的,

这是我自己的错 - 但对于遇到同样问题的人,我在这里回答这个问题作为参考,因为我得到的错误(见我最初的问题) 不明显 :/.

确保在 services.AddMvc:

上配置了 ReferenceLoopHandling.Ignore
// Add framework services.
   services.AddMvc()
        .AddJsonOptions(opt => {
            opt.SerializerSettings.ReferenceLoopHandling =
              ReferenceLoopHandling.Ignore;
        });
    ;