我如何获得的ASP.NET Web API(自托管)上侦听仅* *本地主机? [英] How do I get ASP.NET Web API (self-hosted) to listen on *only* localhost?

查看:101
本文介绍了我如何获得的ASP.NET Web API(自托管)上侦听仅* *本地主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里自托管的ASP.NET Web API服务下面的例子。但是,指定localhost作为在基地址的主机时,它被翻译为+(意为所有可用的)。

I'm following the example here for a self-hosted ASP.NET Web API service. However, when specifying "localhost" as the host in the base address, it is translated to "+" (meaning "all available").

var baseAddress = new Uri("http://localhost:13210");
var configuration = new HttpSelfHostConfiguration(baseAddress);
configuration.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "{controller}/{id}",
    defaults: new {id = RouteParameter.Optional});

using (var server = new HttpSelfHostServer(configuration))
{
    server.OpenAsync().Wait();
    stop.WaitOne();
    server.CloseAsync().Wait();
}

我真的希望我的主人必然只是localhost的 - 它只会从同一台计算机访问,我不想用URL的ACL勾搭

I really do want my host bound to just "localhost" -- it will only be accessed from the same machine, and I don't want to mess around with URL ACLs.

如何配置Web API来的的改写localhost改成+?

How do I configure Web API to not rewrite "localhost" to "+"?

推荐答案

设置 HostNameComparisonMode 属性来精确:

var config = new HttpSelfHostConfiguration("https://localhost/api/");
config.HostNameComparisonMode = HostNameComparisonMode.Exact;

请参阅更多信息,这篇文章上HostNameComparisonMode

See this article for more information on HostNameComparisonMode

这篇关于我如何获得的ASP.NET Web API(自托管)上侦听仅* *本地主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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