asp.net Core多环境读取Json
生活随笔
收集整理的這篇文章主要介紹了
asp.net Core多环境读取Json
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
IHostingEnviroment 獲取環(huán)境相關(guān)洗洗
IsDevelopment()、IsStaging()、IsProduction() 分別為:開(kāi)發(fā)、準(zhǔn)生產(chǎn)、生產(chǎn)環(huán)境
IsEnviroment("Uat") 自定義環(huán)境,比如自定義Uat環(huán)境
新建:
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運(yùn)行
結(jié)果:
?
轉(zhuǎn)載于:https://www.cnblogs.com/jhxk/articles/10057475.html
總結(jié)
以上是生活随笔為你收集整理的asp.net Core多环境读取Json的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 我的穆图性格是稳重,好不好啊?要怎么改性
- 下一篇: BZOJ4568: [Scoi2016]