C#Web-localhost:port有效,127.0.0.1:port不起作用 [英] C# Web - localhost:port works, 127.0.0.1:port doesn't work

查看:124
本文介绍了C#Web-localhost:port有效,127.0.0.1:port不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将C#Web API组件(Web API模型和控制器)添加到现有项目的localhost副本中.

I just finished adding C# Web API components (Web API Models & Controllers) to a localhost copy of an existing project.

此Web API的GET方法应从Android应用中调用.在此链接中,它说明了我应该在Android仿真器上使用10.0.2.2获取计算机的127.0.0.1.

This Web API's GET-methods should be called from an Android app. In this link it's explained I should use 10.0.2.2 on the Android Emulator to get the computer's 127.0.0.1.

当我这样做时,它不适用于Android应用程序中的HttpRequest.因此,我转到Android浏览器并直接键入它,但它也无法正常工作.

When I did this, it didn't work for my HttpRequest in the Android app. So I went to the Android browser and typed it directly, and it also didn't work.

然后我尝试在计算机的浏览器中使用127.0.0.1而不是localhost,并且由于某些未知原因,它也不起作用..localhost127.0.0.1之间有区别吗?我一直以为它们是相同的.

Then I tried using 127.0.0.1 instead of localhost in my computer's browser, and for some unknown reason it also doesn't work.. Is there a different between localhost and 127.0.0.1? I always thought they were one and the same.

这是使用127.0.0.1时得到的400 error:

Here is the 400 error I get when using 127.0.0.1:

使用localhost一切正常.

所以,我的问题是:如何在仿真器上使用localhost(或者,如何解决使用127.0.0.1而不是localhost时遇到的错误)?另外,我想知道localhost127.0.0.1之间的区别,因为我一直认为它们是相同的.

So, my question: How can I use localhost on the Emulator (or, how can I fix the error I get when using 127.0.0.1 instead of localhost)? Also, I would like to know the difference between localhost and 127.0.0.1, since I always thought they were the same.

预先感谢您的答复.

这个stackoverflow问题中,他们提到了Windows System32中的主机文件.我使用Notepad ++(以管理员身份)打开了该文件,并用127.0.0.1 localhost::1 localhost取消了注释行.但是很遗憾,这不能解决问题,我仍然无法在计算机上使用127.0.0.1替代localhost.可能是因为我的问题是相反的(我可以访问localhost,但不能访问127.0.0.1,而不是相反的方式.)

In this stackoverflow question they mention the host file in System32 of Windows. I opened this file with Notepad++ (as Administrator) and uncommented the lines with 127.0.0.1 localhost and ::1 localhost. But unfortunally this didn't fixed the problem and I still can't use 127.0.0.1 on my computer as a replacement for localhost. Probably because my problem is the reversed (I can access localhost, but not 127.0.0.1, instead of the other way around.)

此stackoverflow答案中,解释了localhost127.0.0.1之间的区别是:

In this stackoverflow answer it's explained that the differences between localhost and 127.0.0.1 are:

    在某些编程语言中,
  • 127.0.0.1将更容易被识别为IP.
  • 可以在计算机的主机文件(我的 Edit 1 中提到的文件)中将
  • localhost更改为另一个IP.
  • IPv4和IPv6之间有一些区别.
  • 127.0.0.1 will be more easily recognized as an IP in some programming languages.
  • localhost can be changed to another IP in the computer's host-file (the file mentioned in my Edit 1).
  • There are some differences between IPv4 and IPv6.

所有我现在都已经理解了它们之间的区别,我只是不明白为什么我的本地主机可以工作,但是127.0.0.1却不能.

All and all I kinda understand the differences now, I just don't get why my localhost is working, but 127.0.0.1 isn't..

是否必须对port做一些事情(我将54408用作port)?我已经打开cmd并进行了以下测试:

Does it have to do something with the port (I use 54408 as port)? I've opened cmd and did the following tests:

  • ping localhost:我收到了4次Reply from ::1: time<1ms的回复.
  • ping 127.0.0.1:我收到了4次Reply from 127.0.0.1: bytes=32 time<1ms TIL=128
  • 的回复
  • ping localhost:54408:我遇到错误Ping request could not find localhost:54408. Please check the name and try again.
  • ping 127.0.0.1:54408:我遇到错误Ping request could not find 127.0.0.1:54408. Please check the name and try again.
  • ping localhost: I'm getting a response with 4 times Reply from ::1: time<1ms.
  • ping 127.0.0.1: I'm getting a response with 4 times Reply from 127.0.0.1: bytes=32 time<1ms TIL=128
  • ping localhost:54408: I'm getting an error Ping request could not find localhost:54408. Please check the name and try again.
  • ping 127.0.0.1:54408: I'm getting an error Ping request could not find 127.0.0.1:54408. Please check the name and try again.

就像我之前说过的:127.0.0.1:54408在浏览器中给出了上图所示的错误.并且localhost:54408正常工作..

And like I've said before: 127.0.0.1:54408 in the browser gives the error seen in the image above. And localhost:54408 works just fine..

仍然没有人知道如何解决此问题?

Still no one with an idea on how to fix this?

我的hosts-file的副本,位于C:\Windows\System32\drivers\etc.

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
127.0.0.1   localhost
::1         localhost

最后两行曾经是:

#      127.0.0.1     localhost
#      ::1           localhost


编辑5/半解决方案:


Edit 5 / Semi-Solution:

在杰克C的建议之后,我去寻找Visual Studio端口配置并发现为使用Visual Studio开发服务器的Web应用程序项目指定端口"部分中,我按照说明进行操作,并用我的54408 port将项目属性"中的"Web"选项更改为Use Visual Studio Development Server.

After Jake C's suggestion I went looking for Visual Studio port configuration and found the following site. At the section "To specify a port for a Web application project that uses the Visual Studio Development Server" I followed the instructions and changed the Web option in the Project Properties to Use Visual Studio Development Server with my 54408 port.

这是朝着正确方向迈出的重要一步,因为127.0.0.1:54408主页现在可以使用了.但是,一旦我尝试使用C#网站上的Google OAuth登录,我将收到以下错误:

This was a great step in the right direction, since 127.0.0.1:54408 homepage now works. However, once I try to log in with the Google OAuth on the C# website, I was getting the following error:

我曾在C#网络项目中工作的一位前同事曾告诉我有关

One of my ex-colleagues who worked on the C# web project told me once about this stackoverflow post. In the answer of this post is stated that I should add the redirect-urls (in my case 127.0.0.1) to the Google APIs Console. Right now I don't have access to this Console for my localhost project however, since it's obtained through SVN. I will ask to one of my supervisors for permission to view the Google APIs Console of this C# web project and perhaps edit the redirect-urls to include 127.0.0.1.

一旦它完全起作用,我将接受杰克C的回答,因为他对Http port configurations的解释确实帮助我找到了答案.

Once I've got it completely working I'll accept Jake C's answer, since his explanation of the Http port configurations did indeed help me find the answer.

好的,我已经使用了自己的Google API控制台,并使用新的客户端ID创建了一个新项目.我已经将http://localhost:54408/Account/ExternalLoginCallbackhttp:127.0.0.1:54408/Account/ExternalLoginCallback都添加到了重定向网址中.然后在我的C#Web项目的App_Start/AuthConfig.cs中,我已经更改了客户端"设置以使用该新客户端.

Ok, I've used my own Google APIs Console and created a new Project with a new Client ID. I've added both the http://localhost:54408/Account/ExternalLoginCallback and http:127.0.0.1:54408/Account/ExternalLoginCallback to the redirect-urls. Then in my C# web project's App_Start/AuthConfig.cs I've changed the Client settings to use this new client.

再次感谢Jake C提出的在Visual Studio中更改http port configurations的建议.这帮了我大忙.

Once again thanks Jake C for your suggestion to change the http port configurations in Visual Studio. This did the trick for me.

推荐答案

因此,通常,HTTP 400无效主机名错误通常意味着您没有将网站设置为接受所有主机名和/或IP地址.我想是因为这是一个C#应用程序,所以您要在IIS上托管它.要解决此问题,请打开IIS管理器(Win + R,然后输入inetmgr),依次展开服务器和站点,然后右键单击承载应用程序的网站并选择绑定.在此列表中,应该有一个用于端口54408的http绑定,双击它.在"IP地址"下,确保已选中所有未分配",在主机名"下,确保该字段为空白.点击确定,然后点击关闭.该设置应立即生效,而无需重置IIS.

So typically an HTTP 400 Invalid Hostname error usually means you do not have the website set accept all hostnames and/or IP addresses. I presume because this is a C# application you are hosting this on IIS. To fix this open up IIS Manager (Win+R and enter inetmgr), expand the server then Sites, then right-click on the the website where your application is hosted and select bindings. In this list there should be an http binding for port 54408, double-click on this. Under IP address make sure All Unassigned is selected, and under Host name make sure the field is blank. Hit OK, then Close. The setting should take effect immediately without needing to reset IIS.

如果仅通过Visual Studio的内置Web部署进行测试,则VS中的某处与上述设置相似(我对此有些生疏,所以我不记得确切的位置或方式).另外,您可以按照自己想要的方式在IIS中建立一个网站,然后让VS部署到该网站,而不使用其自己的内部服务器.我认为,以这种方式进行操作可能会带来一些调试方面的缺陷(再次,我对细节有些模糊,在我记住它们或弄清楚它们之后,我将对其进行编辑).

If you are only testing this through Visual Studio's built in web deployment, there are similar settings to the above somewhere in VS (I'm a little rusty on it so I can't remember exactly where or how). Alternatively you can set up a web site in IIS exactly how you want it and then have VS deploy to that website instead of using its own internal server. I think there might be some debugging drawbacks to doing it this way though (once again I'm a little fuzzy on details, I'll edit them in when I remember them or figure it out).

关于这些设置为何存在的一些背景知识: 有时一台服务器需要托管多个站点,这些站点都可以通过端口80进行访问.因此,可以说我们有foo.combar.com,它们太小了,以至于不能保证为它们两个都使用单独的服务器.因此,它们都托管在IP地址为1.2.3.4的服务器上.现在,当您在浏览器中输入URL foo.com并单击go时,它将首先将主机名解析为1.2.3.4,然后创建一个请求,该请求的一部分称为主机头.主机标头中填写了所输入URL的主机名,在本例中为foo.com.服务器收到请求后,它会查看主机头并将其内容发回给foo.com.

A little background as to why these settings exist: Sometimes a server needs to host multiple sites that are all to be accessed via port 80. So lets say we have foo.com and bar.com and they are too small to warrant getting a separate server for both of them. So instead they are both hosted on a sever with an IP address of 1.2.3.4. Now when you enter the URL foo.com into the browser and hit go, it first resolves the host name to 1.2.3.4, and then it creates a request and part of that request is called the host header. The host header is filled with the hostname of the URL entered, in this case foo.com. When the request is received by the server, it looks at the host header and serves back the content for foo.com.

现在,如果要尝试在浏览器中输入1.2.3.4,它将创建一个带有空白主机头的请求,因为未指定任何内容.服务器收到请求后,它不知道该怎么办,因为服务器托管了两个网站,并且没有主机标头指定浏览器要查找的网站,因此它返回错误.

Now if you were to try and enter 1.2.3.4 into the browser, it would create a request with a blank host header, because none was specified. When the request is received by the server, it doesn't know what to do because there are two websites being hosted by the server and there is no host header to specify which one the browser is looking for, so instead it returns an error.

这可能是您遇到的情况.您的网站托管在localhost主机名下,其他任何请求均未得到响应.我指定更改的设置基本上是告诉服务器,无论它使用什么IP地址(网络接口),无论它要查找的主机名是什么(只要它在端口54408上输入)都可以为服务器提供服务.网站.

This is probably what is happening in your situation. Your website is being hosted under the localhost hostname and any other request isn't being responded to. The settings I specified to change are basically telling the server that no matter what IP address (network interface) it comes in on, and no matter what the hostname it is looking for, as long as it is coming in on port 54408, serve this website.

这篇关于C#Web-localhost:port有效,127.0.0.1:port不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆