ASP.NET Core开发-如何配置Kestrel 网址Urls
2016-08-09 12:14
986 查看
ASP.NET Core中如何配置Kestrel Urls呢,大家可能都知道使用UseUrls() 方法来配置。
今天给介绍全面的ASP.NET Core 配置 Urls,使用多种方式配置Urls。
让你了解ASP.NET Core Kestrel 的地址设置。
下面我们就来了解如何配置。我将介绍4种方式来配置Urls。
首先我们新建一个ASP.NET Core 空应用程序。
UseUrls 方法可以使用多个地址,也可以使用一个地址。
单个网址
多个网址
所有的代码都在 Program.cs Main 方法里
使用Kestrel运行程序,http://localhost:5001 http://localhost:5002 http://localhost:5003 均可访问。
首先在项目中添加一个ASP.NET 配置文件hosting.json,在配置文件中加入server.urls 节点。
这里首先需要添加两个引用
然后Program.cs
使用Kestrel运行程序,http://localhost:5001 http://localhost:5002 http://localhost:5003 均可访问。
首先我们需要添加
然后Program.cs
我们到项目目录使用命令
然后访问 http://localhost:5001 http://localhost:5002 http://localhost:5003 均可访问。
我们可以设置系统的环境变量,也可以在VS里设置只用于调试。
首先更改Program.cs
在项目右键属性-》调试 ,这里我们添加变量。
添加一个名称为: LineZero_SERVER.URLS 值为:http://localhost:5001;http://localhost:5002;http://*:5003
然后启动程序,http://localhost:5001 http://localhost:5002 http://localhost:5003 均可访问。
这里我是直接在VS 里添加环境变量,只作用调试环境,如果大家希望系统级别可以使用,就在系统下设置环境变量。
顺便讲解一下 ASPNETCORE_ENVIRONMENT 这里就是环境变量,为何我们在VS 调试的时候看到的 Development,就是这个环境变量控制的。
如果你觉得本文对你有帮助,请点击“推荐”,谢谢。
今天给介绍全面的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 里添加环境变量,只作用调试环境,如果大家希望系统级别可以使用,就在系统下设置环境变量。
顺便讲解一下 ASPNETCORE_ENVIRONMENT 这里就是环境变量,为何我们在VS 调试的时候看到的 Development,就是这个环境变量控制的。
如果你觉得本文对你有帮助,请点击“推荐”,谢谢。
相关文章推荐
- ASP.NET Core开发-如何配置Kestrel 网址Urls
- ASP.NET Core开发-如何配置Kestrel 网址Urls
- ASP.NET Core配置Kestrel 网址Urls
- ASP.NET Core 2.0 : 五.服务是如何加载并运行的, Kestrel、配置与环境
- 配置visual studio code进行asp.net core rc2的开发(转载jeffreywu)
- ASP.NET Core开发-读取配置文件Configuration
- 开发ASP.NET如何将App_Code文件夹中的共享代码配置生成多个Dll
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(七)-- 结构化配置
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
- 如何在 vista 的 iis 7 上面配置 asp.net 1.1 开发环境
- 如何在 vista 的 iis 7 上面配置 asp.net 1.1 开发环境
- Asp.net Core Startup Class中是如何获取配置信息的
- asp.net mvc开发中,web.config文件如何配置数据库链接
- asp.net core 如何在Controller获取配置文件的值
- 【ASP.NET Core】如何隐藏响应头中的 “Kestrel”
- 如何在ASP.NET Core类库项目中读取配置文件详解
- [翻译] 如何在 ASP.Net Core 中使用 Consul 来存储配置
- asp.net core1.x/asp.net core2.0中如何加载多个配置文件
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- ASP.NET Core开发-读取配置文件Configuration