简体   繁体   English

连接到Visual Studio通过LAN调试IIS Express服务器

[英]Connecting to Visual Studio debugging IIS Express server over the lan

I have a test ASP.NET MVC3 application developed in VS2012. 我有一个在VS2012中开发的测试ASP.NET MVC3应用程序。 When I start debugging the app is accessed from the host machine via the request to http://localhost:<portnumber> . 当我开始调试时,通过对http://localhost:<portnumber>的请求从主机访问应用程序。 But if I try to access the same application from the remote machine in the intranet via the http://<ip>:<portnumber> I get HTTP error 400: Bad request. Invalid Host Name. 但是,如果我尝试通过http://<ip>:<portnumber>从Intranet中的远程计算机访问同一个应用程序,我收到HTTP error 400: Bad request. Invalid Host Name. HTTP error 400: Bad request. Invalid Host Name. As far as it runs on IIS Express any server configuration is inaccessible. 就它在IIS Express上运行而言,任何服务器配置都是不可访问的。

Are there any ways of solving this? 有什么方法可以解决这个问题吗?

Update 更新

I made a video that better describes the process, https://youtu.be/5ZqDuvTqQVs 我制作了一个更好地描述过程的视频, https://youtu.be/5ZqDuvTqQVs

If you are using Visual Studio 2013 or above, make sure you run it as an administrator for this to work. 如果您使用的是Visual Studio 2013或更高版本,请确保以管理员身份运行它以使其工作。


Open the %USERPROFILE%\\My Documents\\IISExpress\\config\\applicationhost.config (in VS2015 it may be $(solutionDir)\\.vs\\config\\applicationhost.config ) file. 打开%USERPROFILE%\\My Documents\\IISExpress\\config\\applicationhost.config (在VS2015中可能是$(solutionDir)\\.vs\\config\\applicationhost.config .vs $(solutionDir)\\.vs\\config\\applicationhost.config )文件。 Inside you should see something like this: 在里面你应该看到这样的东西:

 <site name="WebSite1" id="1" serverAutoStart="true"> <application path="/"> <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\\WebSite1" /> </application> <bindings> <binding protocol="http" bindingInformation="*:8080:localhost" /> </bindings> </site> 

Change the bindingInformation=":8080:localhost" to bindingInformation="*:8080:*" (the port number, 8080 in my case, will differ.) bindingInformation=":8080:localhost"更改为bindingInformation="*:8080:*" (端口号,在我的情况下为8080,将有所不同。)

Note: If it does not work try with bindingInformation="*:8080: the asterix can be removed. 注意:如果不起作用,请尝试使用bindingInformation="*:8080:可以删除星号。

Then make sure your firewall is allowing incoming connections on that port. 然后确保您的防火墙允许该端口上的传入连接。 You may need to restart the system or at least Visual Studio to get IISExpress to reload the config file. 您可能需要重新启动系统或至少Visual Studio以使IISExpress重新加载配置文件。

If this doesn't work, take a look at this answer: https://stackoverflow.com/a/5186680/985284 如果这不起作用,请看看这个答案: https//stackoverflow.com/a/5186680/985284

VisualStudio 2015 Non-Admin VisualStudio 2015非管理员

  1. In your solution dir, in the file .vs\\config\\applicationHost.config change the line 在您的解决方案目录中,在.vs\\config\\applicationHost.config文件中更改该行

    <binding protocol="http" bindingInformation="*:44302:localhost" />

    to

    <binding protocol="http" bindingInformation=":44302:" />

    (where 44302 is your port) (44302是你的港口)


  1. From an admin command prompt: 从管理员命令提示符:

    i. 一世。 Enable non-admin to bind to port 启用非管理员绑定到端口

    netsh http add urlacl url=http://*:44302/ user=Everyone

    ii. II。 Allow through firewall 允许通过防火墙

    netsh advfirewall firewall add rule name="IISExpress visualstudio app" protocol=tcp localport=44302 dir=in action=allow


  1. Start debugging from VisualStudio 从VisualStudio开始调试

Except to modify the iisexpress configuration file, sometimes you also need to run the command like below. 除了修改iisexpress配置文件外,有时您还需要运行如下命令。

netsh http add urlacl url=http://*:49419/ user=Everyone netsh http add urlacl url = http:// *:49419 / user = Everyone

Since I am unable to add a comment to @Garret Fogerlie's post and in response to the commenters' issue (@Y.Ecarri and @SamuelEdwinWard), I followed what Garret suggested, using Visual Studio 2013, running it in Admin mode and changing the application.config file. 由于我无法在@Garret Fogerlie的帖子中添加评论并回应评论者的问题(@ Y.Ecarri和@SamuelEdwinWard),我遵循Garret建议的,使用Visual Studio 2013,在管理模式下运行并更改application.config文件。

After launching debug and seeing that I got the same error message, I went back into application.config and saw that a new entry for my site had been created just like Y.Ecarri's issue. 在启动调试并看到我收到相同的错误消息之后,我又回到了application.config并看到我的网站的新条目已经创建,就像Y.Ecarri的问题一样。

So I stopped debugging, kept my solution open in Visual Studio , and edited the application.config file again for the new entry. 所以我停止了调试, 在Visual Studio中保持我的解决方案打开 ,并再次为新条目编辑了application.config文件。 I also simply removed the * sings and localhost entirely, so I had the following for the new entry: 我也完全删除了* sings和localhost ,所以我对新条目有以下内容:

<binding protocol="https" bindingInformation=":44300:" />

How to avoid running Visual Studio as an administrator 如何避免以管理员身份运行Visual Studio

Using both Garret's and @shangkeyun's answer you can achieve connecting to the running website without needing to run Visual Studio as an admin user: 使用Garret@ shangkeyun的答案,您可以实现连接到正在运行的网站,而无需以管理员用户身份运行Visual Studio:

  1. Open %USERPROFILE%\\My Documents\\IISExpress\\config\\applicationhost.config 打开%USERPROFILE%\\My Documents\\IISExpress\\config\\applicationhost.config
  2. Search for your site using name=MySiteName 使用name=MySiteName搜索您的站点
  3. Duplicate the existing <binding> item in the <bindings> section. 复制<bindings>部分中的现有<binding>项。 You should now have two lines with binding . 你现在应该有两行binding
  4. Remove the "localhost" part in bindingInformation. 删除bindingInformation中的“localhost”部分。
  5. It should now look like this, assuming the port is 12345 : 它现在应该是这样的,假设端口是12345

     <binding protocol="http" bindingInformation="*:12345:localhost" /> <binding protocol="http" bindingInformation="*:12345:" /> 
  6. Enable non-admin to bind to port 启用非管理员绑定到端口

     netsh http add urlacl url=http://*:12345/ user=Everyone 

EDIT 2019: gregmac added a step to whitelist the VS instance. 编辑2019:gregmac添加了一个步骤,将VS实例列入白名单。 I never needed this, but listing it anyway: 我从来不需要这个,但无论如何列出它:

  1. netsh advfirewall firewall add rule name="IISExpress visualstudio app" protocol=tcp localport=12345 dir=in action=allow

Thanks to byteit: 感谢byteit:

Go to applicationhost.config in Documents/IISExpress/config 转到Documents / IISExpress / config中的applicationhost.config

find the entry for the particular site you are working on: 找到您正在处理的特定网站的条目:

add: 加:

<binding protocol="http" bindingInformation="*:<your site port>:*" />

in front of the existing 在现有的面前

 <binding protocol="http" bindingInformation="*:<your site port>:localhost" />

To achieve the solution without having VS2013 create a new website xml entry for you when you restart. 要在没有VS2013的情况下实现解决方案,请在重新启动时为您创建新的网站xml条目。 You will need to run as administrator. 您需要以管理员身份运行。

Some of you might spend a lot of time modifying and testing using your %USERPROFILE% directory. 你们中的一些人可能会花费大量时间使用%USERPROFILE%目录进行修改和测试。 If you are running on VS debug , use $(solutionDir).vs\\config\\applicationhost.config 如果您在VS调试上运行,请使用$(solutionDir).vs \\ config \\ applicationhost.config

This is what worked for me: 这对我有用:

  • Start the IIS Manager 启动IIS管理器
  • Add a new virtual directory that points to the projects folder ( C:\\VSProjects in my case) 添加一个指向projects文件夹的新虚拟目录(在我的例子中为C:\\VSProjects
  • Select the new virtual directory within IIS manager. 在IIS管理器中选择新的虚拟目录。 Select Directory Browsing from the list of options. 从选项列表中选择“ Directory Browsing ”。 On the right side there's a Enable button. 在右侧有一个Enable按钮。 Click it. 点击它。

Now I can access my folder and project bin on the network through mypcname\\VSProjects\\myProj\\outputBinViewer . 现在我可以通过mypcname\\VSProjects\\myProj\\outputBinViewer访问网络上的文件夹和项目bin。

After the above configurations, I had to run the Visual Studio in Administrative Mode. 完成上述配置后,我必须在管理模式下运行Visual Studio。

在此输入图像描述

Had the a very similar issue debugging in Visual Studio Code , I solved it by adding: 如果在Visual Studio Code中调试一个非常类似的问题,我通过添加以下内容解决了它:

"env": {
      // ...
      "ASPNETCORE_URLS": "http://*:5000" // change to the port you are using
      // ...
},

.. to launch.json .. to launch.json

Apparently, by default it binds http protocol to 'localhost:5000', so it works with localhost but not with ip address - neither remotely nor locally. 显然,默认情况下它会将http协议绑定到“localhost:5000”,因此它可以与localhost配合使用,但不能与ip地址配合使用 - 既不是远程也不是本地。

If you are trying to hit a breakpoint by a request coming from a different computer, don't forget to check your firewall settings (and/or antivirus) 如果您尝试通过来自其他计算机的请求来查找断点,请不要忘记检查防火墙设置(和/或防病毒)

hope this helps 希望这可以帮助

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

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