我有一个这样的课:
public class RxNormFolderMgr
{
// properties
public string RxNormFolder { get { return ConfigurationSettings.AppSettings["rootFolder"].ToString(); } }
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用它时:
public class TestRxNormFolderManager : ColumnFixture
{
public string RxNormFolder()
{
RxNormFolderMgr folderMgr = new RxNormFolderMgr();
return folderMgr.RxNormFolder;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:"System.Reflection.TargetInvocationException:调用目标抛出异常.---> System.NullReferenceException:对象引用未设置为对象的实例." AppSettings的AllKeys属性是一个零长度的数组,我希望长度为1.
我在项目中的app.config文件如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="rootFolder" value ="C:\RxNorm" />
<!-- Root folder must not end with slash. -->
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我知道ConfigurationSettings.AppSettings应该是过时的,我应该使用ConfigurationManager.AppSettings,但我甚至无法编译.我在项目中有一个参考System.configuration(在我的机器上的c:\ WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll)并在我的代码顶部使用语句.
我正在使用Fitnesse来测试代码,那是我收到错误的时候.我的理解是,我还应该将app.config文件的副本放在我已经完成的测试夹具项目的Bin> Debug文件夹中.所以,我不知道为什么我仍然会收到这个错误.
请帮忙.
我们可以在Web.config文件中存储的连接字符串有两种方式
一种是
<connectionStrings>
<clear/>
<add name="LocalSqlServer"
connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)
另一个是
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=Northwind;uid=sa;password=secret;" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
现在我想知道
这两种方法有什么区别?
哪一个更好的方法?
它们的局限是什么?
更新:你能解释一下<connectionString>有什么重大优势 <appSetting>吗?
我在app.config文件中进行了以下操作:
<appSettings>
<add key="Name" value="Office"/>
...
<add key="Name" value="HotSpot"/>
...
<add key="Name" value="Home"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我试过了
ConfigurationManager.AppSettings["Name"]
Run Code Online (Sandbox Code Playgroud)
但它只给我一个价值?我怎样才能获得所有值的列表?我正在使用c#3.5.是否有lambda表达式或我可以使用的东西来获得它?
在ASP.NET Core 的早期版本中,我们可以动态添加带有环境后缀的 appsetting.json 文件,就像appsettings.Production.json生产环境一样。
由于结构有点不同,看来配置现在已加载到 class 中Program。但是我们这里没有注入``,所以我自己使用环境变量尝试了:
public class Program {
public static void Main(string[] args) {
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) {
string envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
string envConfigFile = $"appsettings.{envName}.json";
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(envConfigFile, optional: true);
var finalConfig = config.Build();
return WebHost.CreateDefaultBuilder(args)
.UseUrls("http://0.0.0.0:5000")
.UseConfiguration(finalConfig)
.UseStartup<Startup>();
}
}
Run Code Online (Sandbox Code Playgroud)
代码已执行,但它不会覆盖我的appsettings.json配置。假设我有以下内容appsettings.json:
{
"MyConnectionString": "Server=xxx,Database=xxx, ..."
}
Run Code Online (Sandbox Code Playgroud)
这个连接字符串正在工作。现在我创建appsettings.Development.json
{
"MyConnectionString": ""
}
Run Code Online (Sandbox Code Playgroud)
并设置ASPNETCORE_ENVIRONMENT=Development. 这肯定会引发异常。但应用程序可以使用来自 …
在我的ASP.NET Core MVC应用程序中,我有一个从AuthorizeAttribute继承并实现IAuthorizationFilter的类。
namespace MyProject.Attributes
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class AllowGroupsAttribute : AuthorizeAttribute, IAuthorizationFilter
{
private readonly List<PermissionGroups> groupList = null;
public AllowGroupsAttribute(params PermissionGroups[] groups)
{
groupList = groups.ToList();
}
public void OnAuthorization(AuthorizationFilterContext context)
{
var executingUser = context.HttpContext.User;
//If the user is not authenticated then prevent execution
if (!executingUser.Identity.IsAuthenticated)
{
context.Result = new StatusCodeResult((int)System.Net.HttpStatusCode.Forbidden);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这使我可以用类似的东西装饰控制器方法 [AllowGroups(PermissionGroups.Admin, PermissionGroups.Level1]
我打算做的是根据列出的枚举值从appsettings.json获取组名,并检查用户是否是这些组的成员。
我的问题是,从属性类中访问应用程序设置的正确方法是什么?
c# appsettings authorize-attribute iauthorizationfilter asp.net-core-mvc
appsettings.json我的项目中有这个文件,如下所示:
{
"ConnectionStrings": {
"MyConnectionString": "Server=SQLSERVER;Database=MyDatabse;Trusted_Connection=True;"
},
"NLog": {
"targets": {
"database": {
"type": "Database",
"dbProvider": "System.Data.SqlClient",
"connectionString": "Server=SQLSERVER;Database=MyDatabse;Trusted_Connection=True;"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不想在多个地方写我的连接字符串。我可以以某种方式引用以前的连接字符串吗?
我已经尝试过:"connectionString": "${appsetting:name=ConnectionStrings.MyConnectionString}",这是行不通的。
我正在 Angular 10 中开发一个 Web 应用程序,想要访问Angular Home 组件中的appsettings.json文件配置。
这是我需要阅读的 appsetting.json 文件配置。
"Application": {
"ServiceUrl": "http://localhost:6000/",
"LogServiceURL": "http://localhost:6002/"
}
Run Code Online (Sandbox Code Playgroud)
请帮助我找出最好的解决方案。
在 C# 中,我们可以将 appSettings 中的一些设置绑定到类,例如:
var connectionStrings = new ConnectionStrings();
var sectionConnectionString = Configuration.GetSection("ConnectionStrings");
Run Code Online (Sandbox Code Playgroud)
在应用程序设置中,它如下所示:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
Run Code Online (Sandbox Code Playgroud)
当我想绑定日志记录时,我需要调用另一个绑定:
Configuration.GetSection("Logging");
Run Code Online (Sandbox Code Playgroud)
如何绑定整个 appsettings 文件?GetSection空字符串不起作用:
Configuration.GetSection("");
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个生成日志文件的C#控制台应用程序.我想对日志文件的存储位置有一定的灵活性.
我尝试使用Settings.settings文件:
名称:logDrive类型:字符串作用域:应用程序值:C:\ Scripts\Logs
在我的代码中,我正在使用:
string logFile = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
logFile = logFile.Replace(@"/", @"-").Replace(@"\", @"-") + ".log";
string logDrive = Properties.Settings.Default.logDrive;
StreamWriter output = new StreamWriter(logDrive + logFile);
Run Code Online (Sandbox Code Playgroud)
在编译上面的时候,我收到错误消息"给定路径的格式不支持".
如果有帮助,则值为:
logDrive ="C:\ Scripts\ServiceDesk\Logs"logFile ="3-23-2009 1:20 PM.log"
有没有人对更好的方法和/或我做错了什么有任何想法/建议?
这有可能吗?例如,通过命名appSettings部分,或嵌套在其他命名部分的appSettings.
我希望实现以下内容:
<section name="development">
<appSettings>
</appSettings>
</section>
<section name="test">
<appSettings>
</appSettings>
</section>
string connectionString
= ConfigurationManager.GetSection("test").AppSettings["connectionString"];
Run Code Online (Sandbox Code Playgroud)
这个模式是什么?
我试图以这种方式保存一个简单的应用程序设置("LanguagePairId"):
if (rdbtnEnglishPersian.IsChecked == true) // because "IsChecked" is a nullable bool, the "== true" is necessary
{
langPairId = 1;
}
else if (rdbtnEnglishGerman.IsChecked == true)
{
langPairId = 2;
}
else if (rdbtnEnglishSpanish.IsChecked == true)
{
langPairId = 3;
}
else if (rdbtnGermanSpanish.IsChecked == true)
{
langPairId = 4;
}
else if (rdbtnGermanPersian.IsChecked == true)
{
langPairId = 5;
}
else if (rdbtnSpanishPersian.IsChecked == true)
{
langPairId = 6;
}
AppSettings.Default.LanguagePairId = langPairId;
Run Code Online (Sandbox Code Playgroud)
LanguagePairId被分配了预期值(如果选中rdbtnEnglishSpanish,则分配3,等等)
但是尝试在应用启动时阅读应用设置值:
int langPairId;
public …Run Code Online (Sandbox Code Playgroud) 我制作了一个自定义中间件,现在我想访问解决方案中另一个项目中的应用程序设置。我是否应该将 IConfiguration 对象注入到中间件构造函数中,并添加 Microsoft.Extensions.Configuration 的 using 语句?或者有更好的方法来做到这一点吗?
我正在使用 Core 2.1 处理 ASP.net 网页。
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using System;
using System.Threading.Tasks;
public class MyMiddleware
{
public IConfiguration _configuration;
public MyMiddleware(RequestDelegate next, IConfiguration config)
{
_next = next;
_ configuration = config;
}
Run Code Online (Sandbox Code Playgroud) 我需要一个 Azure Functions blob 触发器来触发应用设置在运行时提供的存储桶。我读到可以这样做:
[FunctionName("Process")]
public static async Task Process([BlobTrigger("%BucketName%/{name}", Connection = "AzureWebJobsStorage")] Stream avroBlobStream, string name, TraceWriter log)
{
}
Run Code Online (Sandbox Code Playgroud)
如果我BucketName只有Values在 appsettings.json的字段中,这在本地工作。
{
"IsEncrypted": false,
"Values": {
"BucketName": "capture-bucket",
}
}
Run Code Online (Sandbox Code Playgroud)
如果它不在 Values 字段中,则这是错误:
[6/24/2019 5:52:15 PM] Function 'SomeClass.Process' failed indexing and will be disabled.
[6/24/2019 5:52:15 PM] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the …Run Code Online (Sandbox Code Playgroud) appsettings ×13
c# ×7
asp.net ×2
asp.net-core ×2
json ×2
web-config ×2
.net ×1
.net-core ×1
angular ×1
azure ×1
binding ×1
c#-4.0 ×1
fitnesse ×1
middleware ×1
nlog ×1
typescript ×1
wpf ×1