appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
},
"CustomSettings": {
"UseDataCaching": true
}
}
}
期权类别
public class CustomSettings
{
public bool UseDataCaching { get; set; }
}
startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddOptions();
services.Configure<CustomSettings>(Configuration);
...
}
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
控制者
protected IMemoryCache MemoryCache { get; }
protected IOptions<CustomSettings> CustomSettings { get; set; }
public HomeController(IMemoryCache cache, IOptions<CustomSettings> customSettings)
{
MemoryCache = cache;
CustomSettings = customSettings;
}
即使在appsettings.json中,customSettings.Value.UseDataCaching始终为false,但我将其设置为true。
不知道我是否错过了很明显的事情
编辑:添加了启动构造函数
用法:
public IActionResult About()
{
var isUsingDataCache = CustomSettings.Value.UseDataCaching;
ViewData["Message"] = "Your application description page.";
return View();
}
编辑:更改启动以采取2参数
示例项目不起作用
http://www.megafileupload.com/a2hn/WebApplication1.zip
参考方案
不确定如何创建Configuration
对象,但是请确保在其管道中添加appsettings.json
文件,还为应用程序添加BasePath
。例如:
public static IConfigurationRoot Configuration;
// You can use both IHostingEnvironment and IApplicationEnvironment at the same time.
// Instances of them are being injected at runtime by dependency injection.
public Startup(IHostingEnvironment hostingEnv, IApplicationEnvironment appEnv)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
编辑:
尝试更改您的ConfigureServices
方法:
services.Configure<CustomSettings>(Configuration); // from this
services.Configure<CustomSettings>(Configuration.GetSection("CustomSettings")); // to this
编辑2:
您是否可以尝试在控制器的构造函数中创建对象,而无需将其作为IOptions
的通用类型,例如:
private CustomSettings _customSettings;
public YourControllerName(CustomSettings customSettings)
{
_customSettings = customSettings;
}
在Startup.cs
ConfigureServices
方法中,设置:
services.AddSingleton<CustomSettings>();
然后像这样调用它:
public IActionResult About()
{
var isUsingDataCache = _customSettings.UseDataCaching;
ViewData["Message"] = "Your application description page.";
return View();
}
编辑3:
json的CustomSettings
参数放在Logging
参数内。尝试将您的json更改为此:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
}
},
"CustomSettings": {
"UseDataCaching": true
}
}
编辑4:
我实际上尝试过了。创建了一个新的新项目,用Edit 3中的数据替换了默认的appsettings.json
文件。在Startup.cs
方法的第一行中的ConfigureServices
内部:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CustomSettings>(Configuration.GetSection("CustomSettings"));
...
}
最后,我在HomeController
中对其进行了测试:
我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……
ASP.NET-如何更改JSON序列化的方式? - javascript我正在使用ASP.NET通过以下查询返回Json文件:public ActionResult getTransactionTotals(int itemID) { DBEntities db = new DBEntities(); var query = from trans in db.Transactions // Linq query removed …
ASP.NET MVC中应为DataTable返回哪种数据? - javascript我想为DataTable中的每个页面创建动态加载信息。我正在尝试遵循以下示例:https://www.datatables.net/manual/server-sidehttps://www.datatables.net/manual/data来自示例的代码:$('#example').DataTable( { serverSide: t…
asp.net mvc中的对象数组数据始终为null - javascript我需要通过json将对象数组发送到asp.net mvc 2,但是我在mvc控制器中没有得到null对象是这样的entries[1].date = "12/22/2014" entries[1].Ref = "0002" entries[1].Credit = "100" entries[2].da…
ASP.net WebForms-如何从代码隐藏获取html5数据属性? - javascript即时通讯试图从HtmlControl获取数据属性(数据图标)...该数据属性是通过js函数设置的,但是当页面回发时,它返回(在代码后面)一个空字符串有什么办法,任何财产,否则就可以得到它?我的代码:HTML:<button id="btnIcon" runat="server" class="btn b…