加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 远程连接到.net核心自托管的web api

发布时间:2020-12-15 17:13:38 所属栏目:百科 来源:网络整理
导读:我有一个简单的.net核心web api,只有一个动作: [Route("[action]")]public class APIController : Controller{ // GET api/values [HttpGet] public string Ping() { return DateTime.Now.ToString(); }} 如果我通过dotnet运行这个运行,我得到 Hosting envi
我有一个简单的.net核心web api,只有一个动作:
[Route("[action]")]
public class APIController : Controller
{
    // GET api/values
    [HttpGet]
    public string Ping()
    {
        return DateTime.Now.ToString();
    }
}

如果我通过dotnet运行这个运行,我得到

Hosting environment: Production
Content root path: C:UsersxxxDocumentsVisual Studio 2015ProjectsSelfHostTestsrcSelfHostTest
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

转到浏览器并输入http://localhost:5000/ping会导致成功返回当前时间.但是,转到远程计算机(相同的LAN)并尝试通过http://odin:5000/ping访问服务会导致404错误. (Odin是通过dotnet运行在控制台中运行web api的机器的名称).

服务器(和客户端!)防火墙都已关闭.我可以成功地ping“odin”.

任何想法我在这里缺少什么简单的步骤.我在家里和工作中都尝试过这个但没有成功.

解决方法

我的猜测是问题不在你的控制器中,而是在program.cs中.您需要修改WebHost的构造
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseUrls("http://localhost:5000","http://odin:5000","http://192.168.1.2:5000")
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

除非你添加UseUrls行,否则Kestrel不会在localhost之外监听.这是有道理的,因为在正常情况下,Kestrel将坐在IIS或NGNIX等反向代理之后,不需要绑定到外部URL.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读