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

时间:2021-09-15 14:27:12

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();
}

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

使用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"

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

然后访问 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

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

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

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

顺便讲解一下 ASPNETCORE_ENVIRONMENT 这里就是环境变量,为何我们在VS 调试的时候看到的 Development,就是这个环境变量控制的。

如果你觉得本文对你有帮助,请点击“推荐”,谢谢。

ASP.NET Core开发-如何配置Kestrel 网址Urls的更多相关文章

  1. ASP&period;NET Core配置Kestrel 网址Urls

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

  2. 配置Kestrel 网址Urls

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

  3. &lbrack;ASP&period;NET Core开发实战&rsqb;基础篇06 配置

    配置,是应用程序很重要的组成部分,常常用于提供信息,像第三方应用登录钥匙.上传格式与大小限制等等. ASP.NET Core提供一系列配置提供程序读取配置文件或配置项信息. ASP.NET Core项 ...

  4. ASP&period;NET Core开发-后台任务利器Hangfire使用

    ASP.NET Core开发系列之后台任务利器Hangfire 使用. Hangfire 是一款强大的.NET开源后台任务利器,无需Windows服务/任务计划程序. 可以使用于ASP.NET 应用也 ...

  5. 图解 ASP&period;NET Core开发环境准备

    2016年6月28日微软宣布发布 .NET Core 1.0.ASP.NET Core 1.0 和 Entity Framework Core 1.0. .NET Core是微软在两年前发起的开源跨平 ...

  6. windows&sol;Linux下设置ASP&period;Net Core开发环境并部署应用

    10分钟学会在windows/Linux下设置ASP.Net Core开发环境并部署应用 创建和开发ASP.NET Core应用可以有二种方式:最简单的方式是通过Visual Studio 2017 ...

  7. ASP&period;NET Core macOS 环境配置 - ASP&period;NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core macOS 环境配置 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 是对 ASP.NET 有重大意义的一次重新设计.本章节我 ...

  8. ASP&period;NET Core Windows 环境配置 - ASP&period;NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Windows 环境配置 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Windows 环境配置 ASP.NET Core ...

  9. ASP&period;NET Core Windows 环境配置

    ASP.NET Core 是对 ASP.NET 有重大意义的一次重新设计.本章节我们将介绍 ASP.NET Core 中的一些新的概念和它们是如何帮助我们开发现代化的 Web 应用程序 尽管 ASP. ...

随机推荐

  1. UICollectionView中使用 UICollectionViewFlowLayout进行布局(模仿苹果相册)

    现在都知道,在初始化UICollectionView的时候,必须要传入一Layout对象,进行布局管理.这也是collectionview和tableview的明显区别,通过collectionvie ...

  2. 激活Microsoft Office professional plus 2010

    1. 下载工具 http://pan.baidu.com/s/1dDDFhEL 2. 启动工具 双击Office 2010 Toolkit.exe 在Activation选项栏中选择AutoKMS C ...

  3. Locking

    Computer Science An Overview _J. Glenn *shear _11th Edition To solve such problems, a DBMS could ...

  4. offset&sol;client&sol;scroll一些总结

    offset/client/scroll一些总结 1.offset 首先offset共有五个值 1.offsetParent 2.offsetTop 3.offsetLeft 4.offsetWidt ...

  5. eclipse集成maven

    1.工具下载: Eclipse4.2 jee版本(这里使用最新的Eclipse版本,3.7以上版本按照以下步骤都可以) 下载地址:http://www.eclipse.org/downloads/do ...

  6. 用iptables 做NAT代理上网

    背景:有一台A服务器不能上网,和B服务器通过内网来连接,B服务器可以上网,要实现A服务器也可以上网. 内网主机: A eth1:172.16.1.8 外网主机: B eth0:10.0.0.61外网主 ...

  7. django restframework

    一.django restframework 请求流程源码剖析 上面的认证一个流程是rest_framework的关于APIauth的认证流程,,这个流程试用权限.频率.版本.认证.这个四个组件都是通 ...

  8. Bytom合约预编译

    比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 在开发合 ...

  9. 再谈:自定义结构体的对齐问题之&lowbar;&lowbar;attribute&lowbar;&lowbar; &lpar;&lpar;packed&rpar;&rpar;方法【转】

    转自:https://blog.csdn.net/ipromiseu/article/details/5955295 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.c ...

  10. go协程使用陷阱&lpar;转&rpar;

    协程中使用全局变量.局部变量.指针.map.切片等作为参数时需要注意,此变量的值变化问题. 与for 循环,搭配使用更需谨慎. 1,内置函数时直接使用局部变量,未进行参数传递 package main ...