asp.net Core多环境读取Json
生活随笔
收集整理的這篇文章主要介紹了
asp.net Core多环境读取Json
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IHostingEnviroment 獲取環境相關洗洗
IsDevelopment()、IsStaging()、IsProduction() 分別為:開發、準生產、生產環境
IsEnviroment("Uat") 自定義環境,比如自定義Uat環境
新建:
appsettings.Uat.json文件
{"Enviroment": "Uat"
}
Controller文件:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration;namespace WebApplication1.Controllers {[Route("[Controller]")]public class EnviromentController : Controller{private readonly IConfiguration _configuration;public EnviromentController(IConfiguration configuration){_configuration = configuration;}[HttpGet("Index")]public IActionResult Index(){String enviroment=_configuration["Enviroment"];return View(nameof(Index), enviroment);}} }
view文件:
@model string
@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment hostEnvi
@{Layout = null;
}<!DOCTYPE html><html>
<head><meta name="viewport" content="width=device-width" /><title>Index</title>
</head>
<body><h1>@hostEnvi.EnvironmentName</h1><h1>@Model</h1>
</body>
</html>
在launchSettings.json文件profiles下中添加:
"Uat": {"commandName": "Project","launchBrowser": true,"applicationUrl": "http://localhost:5000","environmentVariables": {"ASPNETCORE_ENVIRONMENT": "Uat"}}
選擇Uat運行
結果:
?
轉載于:https://www.cnblogs.com/jhxk/articles/10057475.html
總結
以上是生活随笔為你收集整理的asp.net Core多环境读取Json的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的穆图性格是稳重,好不好啊?要怎么改性
- 下一篇: BZOJ4568: [Scoi2016]