Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core mvc 如何读取.NET Core中属性内部的配置(appsettings)值?_Asp.net Core Mvc_Custom Attributes_Appsettings_Asp.net Core 1.1 - Fatal编程技术网

Asp.net core mvc 如何读取.NET Core中属性内部的配置(appsettings)值?

Asp.net core mvc 如何读取.NET Core中属性内部的配置(appsettings)值?,asp.net-core-mvc,custom-attributes,appsettings,asp.net-core-1.1,Asp.net Core Mvc,Custom Attributes,Appsettings,Asp.net Core 1.1,我有一个.NET Core 1.1应用程序,在HomeController中的某个操作上设置了自定义属性。鉴于我需要属性逻辑中的配置文件(appsettings.json)中的值,是否可以访问属性级别的配置 appsettings.json { "Api": { "Url": "http://localhost/api" } } HandleSomethingAttribute.cs public class HandleSomethingAttribute :

我有一个.NET Core 1.1应用程序,在HomeController中的某个操作上设置了自定义属性。鉴于我需要属性逻辑中的配置文件(appsettings.json)中的值,是否可以访问属性级别的配置

appsettings.json

{
    "Api": {
        "Url": "http://localhost/api"
    }
}
HandleSomethingAttribute.cs

public class HandleSomethingAttribute : Attribute, IActionFilter
{
    public void OnActionExecuting(ActionExecutingContext context)
    {
        // read the api url somehow from appsettings.json
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {
    }
}
public class HomeController: Controller
{
     [HandleSomething]
     public IActionResult Index()
     {
         return View();
     }
}
HomeController.cs

public class HandleSomethingAttribute : Attribute, IActionFilter
{
    public void OnActionExecuting(ActionExecutingContext context)
    {
        // read the api url somehow from appsettings.json
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {
    }
}
public class HomeController: Controller
{
     [HandleSomething]
     public IActionResult Index()
     {
         return View();
     }
}

您好,请在属性构造函数中尝试此操作。它应该能工作

我也在做同样的事情。我做了一些与的解决方案类似的事情,但为了获得环境名称,我使用了
environment.GetEnvironmentVariable(“ASPNETCORE\u-environment”)
。我把它放在静态类中的一个静态变量中,我可以从项目中的任何地方读取它

public static class AppSettingsConfig
{
    public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
       .SetBasePath(Directory.GetCurrentDirectory())
       .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
       .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
       .Build();
}
我可以这样从属性调用它:

public class SomeAttribute : Attribute
{
    public SomeAttribute()
    {
        AppSettingsConfig.Configuration.GetValue<bool>("SomeBooleanKey");
    }
}
public类SomeAttribute:Attribute
{
公共属性()
{
appsetingsconfig.Configuration.GetValue(“SomeBooleanKey”);
}
}

你能分享一些你所拥有的和你想要完成的事情的代码吗?@Shoe在这里看到同样的问题……你能解决吗?@Dzhambazov还没有……我如何访问环境名称?因为我对开发和生产有不同的应用程序设置