【问题标题】:Remote Access to Local ASP.NET Core Applications From IP Address?从 IP 地址远程访问本地 ASP.NET Core 应用程序?
【发布时间】:2019-10-12 05:36:52
【问题描述】:

我想从远程计算机或移动设备(调用)调试我的 asp.net 核心应用程序?请帮我。在标准 .net 中,当将 <binding protocol="http" bindingInformation="*:21918:localhost" /> 更改为 <binding protocol="http" bindingInformation="*:21918:*" /> 时,将此代码添加到 .vs\config\applicationhost.config 中。

           <site name="project" id="2">
                <application path="/" applicationPool="Clr4IntegratedAppPool">
                    <virtualDirectory path="/" physicalPath="F:\users\Api" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:21918:*" /> 
                </bindings>
            </site>

【问题讨论】:

    标签: asp.net-mvc visual-studio api model-view-controller


    【解决方案1】:

    你可以使用这个方法:

    在您的项目中打开 Program.cs:

            var configuration = new ConfigurationBuilder()
                .AddCommandLine(args)
                .Build();
    
    
            var hostUrl = configuration["hosturl"]; // add this line
            if (string.IsNullOrEmpty(hostUrl)) // add this line
                hostUrl = "http://0.0.0.0:5001"; // add this line
    
    
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseUrls(hostUrl)   // // add this line
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseConfiguration(configuration)
                .Build();
    
            host.Run();
    

    More Info

    【讨论】:

    • UseUrls 不会使本地运行的 Net Core api 在连接到同一网络的 Android 设备上可用。
    【解决方案2】:

    对于这种情况有一个隧道解决方案,您可以使用ngrok 应用程序来隧道您的本地环境,并为网络外部提供一个远程 URL 以进行调试或测试。

    下载 ngrok 后,只需运行以下命令:

    ./ngrok http 25114 -host-header="localhost:25114"
    

    别忘了替换你的本地应用端口。

    【讨论】:

      【解决方案3】:

      您可以按照以下说明解决您的问题并解决所有这些问题。 1- 确保您没有任何打开的 Visual Studio 实例。

      2- 打开文件 %userprofile%\Documenti\IISExpress\config\applicationhost.config ,即 %userprofile% 您的用户文件夹 – 就像 C:\Users\YourUsername\.

      3-查找与我们要修改的Web应用程序对应的条目并通过以下方式更改其绑定元素: &lt;binding protocol="http" bindingInformation="*:&lt;port&gt;:*" /&gt; 我们需要做的就是用 * 替换几个 localhost,让自动分配的 &lt;port&gt; 保持原样。如果您确实需要更改该端口,请将其替换为另一个有效的空闲 TCP 端口(如 8080)并执行以下步骤。否则,跳过它并转到它旁边的步骤。

      4-如果您更改了自动分配的 TCP 端口,您还需要打开您的 Web 应用程序项目 (.csproj) 和解决方案 (.sln) 文件,并确保没有对先前端口的引用:它们是否存在取决于您选择的项目类型(Web 应用程序、MVC 应用程序、网站等)。如果是这种情况,请将旧端口替换为新端口。

      5-打开命令提示符(具有管理员权限)并键入以下内容: netsh http add urlacl url=http://*:&lt;port&gt;/ user=everyone 将应用程序 TCP 端口替换为 .如果您收到错误消息 (1789),则表示您的系统上不存在 Everyone 用户组,在某些 Windows 本地化版本中可能会出现这种情况。如果是这种情况,只需将所有人替换为相应的用户组名称即可。除了执行此步骤,您还可以尝试使用管理员权限执行 Visual Studio。

      6-打开 Windows 防火墙高级配置面板并添加入站规则以启用应用程序 IISExpress.exe 或 Web 应用程序使用的 TCP 端口的入站流量。如果您为其他产品禁用它,请对它执行相同操作。如果你不使用任何——你确定吗? – 您可以抓住机会填补空白,然后执行上述操作,也可以跳过这一步。

      完成此操作后,您可以启动 Visual Studio 并在调试或发布模式下运行您的应用程序:您应该能够使用以下网址从任何外部的网络连接设备访问它: http://&lt;lan-ip-address&gt;:&lt;port&gt;/

      【讨论】:

      • 我使用了你的指令,但是为了让它工作,我在&lt;solution path&gt;\.vs\config 中编辑了applicationhost.config 而不是用户文件夹下的文档路径。
      猜你喜欢
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 2020-07-27
      • 1970-01-01
      相关资源
      最近更新 更多