简体   繁体   English

自托管WebAPI请求两次

[英]Self Hosted WebAPI requested twice

I'm learning about self-hosted web api, owin/katana and I came with this example: 我正在学习自托管的Web api,owin / katana,并附带以下示例:

Source: http://pastebin.com/0hsiEsdP 资料来源: http//pastebin.com/0hsiEsdP

I pasted it on pastebin because here I keep getting "your code is not formatted" even between code tags =\\ 我将其粘贴到pastebin上,因为即使在代码标签= \\之间,我仍然会得到“您的代码未格式化”

So, there's two middlewares in the pipeline, one for "logging" and the other to just write something as response. 因此,管道中有两种中间件,一种用于“记录”,另一种只是写一些东西作为响应。

When I execute it and open the url in browser, it prints the "logging" message twice, like this: 当我执行它并在浏览器中打开URL时,它将两次打印“日志记录”消息,如下所示:

Server started [Request from ::1] [Request from ::1] 服务器已启动[来自:: 1的请求] [来自:: 1的请求]

The question is: why it's executed twice if I did only one request ? 问题是:如果只执行一个请求,为什么要执行两次?

Thanks! 谢谢!

If you log the request uri, like this: 如果您记录请求uri,如下所示:

Console.WriteLine("[Request from {0} to {1}]", context.Request.RemoteIpAddress, context.Request.Uri);

You'll see that there are in fact two requests: 您会看到实际上有两个请求:

[Request from ::1 to http://localhost:8080/]
[Request from ::1 to http://localhost:8080/favicon.ico]

The second one is to get the little icon that is displayed in the browser address bar. 第二个是获取显示在浏览器地址栏中的小图标。 Also, this is browser-dependant: firefox will try to get favicon.ico once, and then will issue just single requests. 此外,这取决于浏览器:firefox将尝试获取favicon.ico一次,然后仅发出单个请求。 Chome is always trying to get the favicon. Chome一直在尝试获取收藏夹图标。

For testing purpose, you can use Fiddler to peek the http traffic and see what's excatly going on. 出于测试目的,您可以使用Fiddler窥视http流量并查看正在发生的事情。 Also, you can use some tools to issue raw HTTP requests directly (Fiddler, Postman, wget), because browsers can do stuff that you don't need or want while testing (like getting the favicon, caching, etc). 另外,您可以使用某些工具直接发出原始HTTP请求(Fiddler,Postman,wget),因为浏览器可以执行测试时不需要或不需要的东西(例如获取favicon,缓存等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM