配置Kestrel 网址Urls

ASP.NET Core中如何配置Kestrel Urls呢,大家可能都知道使用UseUrls() 方法来配置。

今天给介绍全面的ASP.NET Core 配置 Urls,使用多种方式配置Urls。

让你了解ASP.NET Core Kestrel 的地址设置。

下面我们就来了解如何配置。我将介绍4种方式来配置Urls。

首先我们新建一个ASP.NET Core 空应用程序。

UseUrls

大家最熟悉的一种也就是使用UseUrls 。下面我们就来实际使用。

UseUrls 方法可以使用多个地址,也可以使用一个地址。

单个网址

UseUrls("http://localhost:5001")

多个网址

UseUrls("http://localhost:5001", "http://localhost:5002", "http://*:5003")//多个地址 *代表绑定所有本机地址 可以局域网访问,拥有外网ip 就可以外网访问

所有的代码都在 Program.cs Main 方法里

        public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls("http://localhost:5001", "http://localhost:5002", "http://*:5003")
.UseStartup<Startup>()
.Build(); host.Run();
}

使用Kestrel运行程序,http://localhost:5001 http://localhost:5002 http://localhost:5003 均可访问。

配置文件

下面使用配置文件来设置网址。

首先在项目中添加一个ASP.NET 配置文件hosting.json,在配置文件中加入server.urls 节点。

{
"server.urls": "http://localhost:5001;http://localhost:5002;http://*:5003"
}

这里首先需要添加两个引用

    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0"

然后Program.cs

        public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: true)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); host.Run();
}

使用Kestrel运行程序,http://localhost:5001 http://localhost:5002 http://localhost:5003 均可访问。

命令行参数

下面使用命令行参数来设置网址。

首先我们需要添加

"Microsoft.Extensions.Configuration.CommandLine": "1.0.0"

然后Program.cs

        public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddCommandLine(args)
.Build(); var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); host.Run();
}

我们到项目目录使用命令

dotnet run --server.urls "http://localhost:5001;http://localhost:5002;http://*:5003"

然后访问 http://localhost:5001 http://localhost:5002 http://localhost:5003 均可访问。

环境变量

这里我们可以通过环境变量来设置网址。

我们可以设置系统的环境变量,也可以在VS里设置只用于调试。

首先更改Program.cs

        public static void Main(string[] args)
{
//环境变量
var config = new ConfigurationBuilder()
.AddEnvironmentVariables("LineZero_")
.Build(); var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); host.Run();
}

在项目右键属性-》调试 ,这里我们添加变量。

添加一个名称为: LineZero_SERVER.URLS 值为:http://localhost:5001;http://localhost:5002;http://*:5003

然后启动程序,http://localhost:5001 http://localhost:5002 http://localhost:5003 均可访问。

这里我是直接在VS 里添加环境变量,只作用调试环境,如果大家希望系统级别可以使用,就在系统下设置环境变量。

配置Kestrel 网址Urls的更多相关文章

  1. ASP.NET Core开发-如何配置Kestrel 网址Urls

    ASP.NET Core中如何配置Kestrel Urls呢,大家可能都知道使用UseUrls() 方法来配置. 今天给介绍全面的ASP.NET Core 配置 Urls,使用多种方式配置Urls. ...

  2. ASP.NET Core配置Kestrel 网址Urls

    ASP.NET Core中如何配置Kestrel Urls呢,大家可能都知道使用UseUrls() 方法来配置. 今天给介绍全面的ASP.NET Core 配置 Urls,使用多种方式配置Urls.让 ...

  3. Net Core -- 配置Kestrel端口

    Net Core -- 配置Kestrel端口 Kestrel介绍 在Asp.Net Core中,我们的web application 其实是运行在Kestrel服务上,它是一个基于libuv开源的跨 ...

  4. ASP.NET Core中配置监听URLs的五种方式

    原文: 5 ways to set the URLs for an ASP.NET Core app 作者: Andrew Lock 译者: Lamond Lu 默认情况下,ASP. NET Core ...

  5. Do you kown Asp.Net Core -- 配置Kestrel端口

    Kestrel介绍 在Asp.Net Core中,我们的web application 其实是运行在Kestrel服务上,它是一个基于libuv开源的跨平台可运行 Asp.Net Core 的web服 ...

  6. Django视图层之路由配置系统(urls)

    视图层之路由配置系统(urls) URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个U ...

  7. 在.NET 6.0上使用Kestrel配置和自定义HTTPS

    大家好,我是张飞洪,感谢您的阅读,我会不定期和你分享学习心得,希望我的文章能成为你成长路上的垫脚石,让我们一起精进. 本章是<定制ASP NET 6.0框架系列文章>的第四篇.在本章,我们 ...

  8. ASP.NET Core的Kestrel服务器

    原文地址----Kestrel server for ASP.NET Core By Tom Dykstra, Chris Ross, and Stephen Halter Kestrel是一个基于l ...

  9. 【原生态跨平台:ASP.NET Core 1.0(非Mono)在 Ubuntu 14.04 服务器上一对一的配置实现-篇幅1】

    鸡冻人心的2016,微软高产年. build 2016后 各种干货层出不穷. 1 Win10 集成了bash  ,实现了纳德拉的成诺,Microsoft Love Linux!!! 2 跨平台  ,收 ...

随机推荐

  1. angularjs学习笔记—事件指令

    ngClick 适用标签:所有触发条件:单击 #html <div ng-controller="LearnCtrl"> <div ng-click=" ...

  2. ios打包ipa的四种实用方法(.app转.ipa)-备

    感谢大神分享这个博客 总结一下,目前.app包转为.ipa包的方法有以下几种: 1.Apple推荐的方式,即实用xcode的archive功能 Xcode菜单栏->Product->Arc ...

  3. ASP.NET WEB API 如何使用基于Post的方式传递多个值(二)

    前面我曾经写过一篇文章,是基于HttpContext的请求上下文中读取表单参数,其实还可以将其单独拆分出来. 基于Filter的方式 获取表单值:(核心代码)   public void OnActi ...

  4. 简要介绍如何集成Vitamio安卓版SDK

    1.下载VitamioBundle的最新稳定,这里下载的是最新版4.2.2. 2.解压缩后,导入 Vitamio 库工程(即vitamio)和Demo工程(即vitamio--sample)到 Ecl ...

  5. BOT、BT、PPP形式介绍(1)

    BOT.BT.PPP形式介绍 BOT1.什么是BOT     BOT是英文Build-Operate-Transfer的缩写,即“建设-经营-转让”.实质上是基础设施投资.建设和经营的一种方式,以政府 ...

  6. VMWare 虚拟机

    http://pan.baidu.com/share/link?shareid=5873&uk=941708466 VMWare是虚拟化解决方案厂商,旗下有多款虚拟机软件产品,其中最知名的要数 ...

  7. 读书笔记:java特种兵(上)

    ----看着样章,感觉还不错,就买下来了,书先不论好坏,悟到了一个道理,东西没有好与坏,只有适不适合. 第一章:想了解编译器是如何优化程序的,当年的编译原理没有学好啊

  8. [每日一题] 11gOCP 1z0-052 :2013-09-27 bitmap index.................................................C37

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12106027 正确答案C 这道题目是需要我们掌握位图索引知识点. 一.首先我们来看位图索引 ...

  9. tool - 支持TestLink 1.93,将excel格式用例转化成可以导入的xml格式

     tool - 支持TestLink 1.93,将excel格式用例转化成可以导入的xml格式  https://github.com/zhangzheyuk/CaseConvert

  10. python RabbitMQ队列使用(入门篇)

    ---恢复内容开始--- python RabbitMQ队列使用 关于python的queue介绍 关于python的队列,内置的有两种,一种是线程queue,另一种是进程queue,但是这两种que ...