【问题标题】:AspNetCore WebApi QueryString parameter is processed but browser & postman say "Server not found"AspNetCore WebApi QueryString 参数已处理,但浏览器和邮递员说“找不到服务器”
【发布时间】:2017-03-15 22:57:02
【问题描述】:

更新:对于有相同错误/行为的人,请参阅答案 stackoverflow.com/a/42813181/1155847 - 我自己的愚蠢错误,但错误消息 - 好吧 没有给出真正的错误消息实际上.. 这么说吧,行为很奇怪..至少,现在解决了


原始问题/问题

我正在学习使用 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 时,我在返回参数时遇到问题 - 邮递员和浏览器都给我返回“未找到服务器”,浏览器甚至重定向到 @987654327 @... 对我来说奇怪的行为,因为其他请求,例如 localhost:8088/api/camps/1localhost:8088/api/camps 都可以正常工作。

如果我调试并单步执行,我发现它运行良好并获取 QueryString,对数据库执行它,甚至点击 return Ok(camp); 行...但我从来没有得到任何返回?

提琴手

  • 提琴手说[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes.

请求计数:1 发送字节:408(标头:408;正文:0)字节 收到:717(标题:205;正文:512)

实际表现

注意:此请求在接收操作失败后重试。

ClientConnected:14:20:36.281 ClientBeginRequest:14:20:44.067 GotRequestHeaders:14:20:44.067 ClientDoneRequest:14:20:44.067 确定网关:0 毫秒 DNS 查找:0 毫秒 TCP/IP 连接:0 毫秒 HTTPS 握手:0ms 服务器连接:14:20:44.294 FiddlerBeginRequest:14:20:44.294 ServerGotRequest:14:20:44.294 ServerBeginResponse: 00:00:00.000 GotResponseHeaders: 00:00:00.000 服务器完成响应:14:20:44.537 客户端开始响应:14:20:44.537 ClientDoneResponse: 14:20:44.538

总耗时:0:00:00.470

响应字节(按内容类型) -------------- text/html: 512 ~headers~: 205

环境

在 Windows 10、Visual Studio 2017、IIS Express、localhost 端口 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 来初始化数据库,否则你将无法如果需要,可以运行示例。

【问题讨论】:

    标签: c# asp.net .net-core asp.net-web-api-routing


    【解决方案1】:

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

    【讨论】:

    • 我确实一直在使用邮递员,并且确实使用了 http 前缀。 http://localhost:8088/api/camps/1 运行良好,当我运行 ?includeSpeakers=true 添加到它时,响应失败。请求通过“OK”和右括号。我运行了您的“之后”示例代码,效果很好,但使用了 1.0.3 和 1.0.2 库 + xproj。虽然我从 VS2017 开始,但 IDK 是否与它有关 - VS2017 附带的 IISExpress 可能需要其他配置?我将尝试在 Vs2015 中重新启动,看看是否有同样的问题。尽管如此,还是感谢您的帮助!
    • 查看答案stackoverflow.com/a/42813181/1155847 - 我自己的愚蠢错误,但错误消息很奇怪(没有错误:))
    • @ShawnWildermuth 我一直在关注你的复数课程,但有一个问题,希望你能帮助我:stackoverflow.com/questions/43764279/…
    【解决方案2】:

    好的,

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

    确保您在services.AddMvc 上配置了ReferenceLoopHandling.Ignore

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

    【讨论】:

    • 是的,这是一个关于序列化发生方式的已知问题。它过去常常因无休止地序列化循环而失败。
    猜你喜欢
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2021-03-10
    • 2020-04-21
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多