C# 属性 AppSettings

问题描述 投票:0回答:1

我正在使用 .NET Web API(4.6 Framework)编写应用程序

我有一个属性,我使用:

[ApiExplorerSettings(IgnoreApi = true)]
来隐藏我的 Swagger 中的某些控制器。

此属性是 :

System.Web.Http.Description

的一部分

基本上,我想在 web.config 文件中创建一个 AppSetting,以便当我发布到开发时,控制器会显示

(IgnoreApi = false)
,而当我发布到生产时,控制器会隐藏
(IgnoreApi = true)

我尝试直接在属性中访问

ConfigurationManager.AppSettings
,但这似乎无法按预期工作。

也许我需要找到一种方法来覆盖该属性,以便在 IgnoreApi 的 getter/setter 上,它可以从我的 web.config 中提取正确的值?

c# asp.net .net swashbuckle
1个回答
0
投票

扩展“ApiExplorerSettingsAttribute”类看起来很简单,但它是密封的。因此最终采用了以下解决方法;

  • 从基类“Attribute”继承的自定义属性。

includeInApiExplorerAttribute.cs 类

public class IncludeInApiExplorerAttribute : Attribute
{
    private readonly bool value;
    public IncludeInApiExplorerAttribute(string IsInAPI=null)
    {
        if (!string.IsNullOrEmpty(IsInAPI))
        {
            value = Convert.ToBoolean(ConfigurationManager.AppSettings[IsInAPI]); //Reads the app config value
        }
        else
        {
            value = true;
        }
    }
    public bool Value { get { return value; } }
}
  • 然后我们可以实现一个自定义的 ApiExplorer,如下所示。

OptApiExplorer.cs 类

    public class OptApiExplorer : ApiExplorer
{
    public OptApiExplorer(HttpConfiguration configuration)
        : base(configuration)
    {

    }

    //Overrides the method from the base class
    public override bool ShouldExploreAction(string actionVariableValue, HttpActionDescriptor actionDescriptor, IHttpRoute route)
    {
        var includeAttribute = actionDescriptor.GetCustomAttributes<IncludeInApiExplorerAttribute>().FirstOrDefault(); //Get the given custom attribute from the action
        if (includeAttribute != null)
        {
            return includeAttribute.Value && MatchRegexConstraint(route, "action", actionVariableValue); //If it is not null read the includeAttribute.Value which is set in app.config and return true or false based on the includeAttribute.Value and MatchRegexConstraint return value
        }
        var includeControlAttribute = actionDescriptor.ControllerDescriptor.GetCustomAttributes<IncludeInApiExplorerAttribute>().FirstOrDefault(); //If the action does not have any given type of custom attribute then chekc it in the controller level
        if (includeControlAttribute != null)
        {
            return includeControlAttribute.Value && MatchRegexConstraint(route, "action", actionVariableValue);//Similar to action level
        }
        return true && MatchRegexConstraint(route, "action", actionVariableValue);
    }


    //This method is as it is in the base class
    private static bool MatchRegexConstraint(IHttpRoute route, string parameterName, string parameterValue)
    {
        IDictionary<string, object> constraints = route.Constraints;
        if (constraints != null)
        {
            object constraint;
            if (constraints.TryGetValue(parameterName, out constraint))
            {
                string constraintsRule = constraint as string;
                if (constraintsRule != null)
                {
                    string constraintsRegEx = "^(" + constraintsRule + ")$";
                    return parameterValue != null && Regex.IsMatch(parameterValue, constraintsRegEx, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                }
            }
        }
        return true;
    }
}
  • 网页配置设置

这是我们的自定义属性读取的值。将其添加到 web.config 文件中

  <appSettings>
    <add key="IsInAPI" value="false"/>
  </appSettings>
  • 在WebAPI.config.cs文件中添加以下内容

我们用自定义类替换了 IApiExplorer。

      config.Services.Replace(typeof(IApiExplorer), new OptApiExplorer(config));
  • 然后在您的控制器或操作中您可以添加自定义 属性如下。

    [IncludeInApiExplorer("IsInAPI")]
    

    IsInApi 是 web.config 值,我们可以将其设置为 true 或 false。如果未设置,则它将默认设置为 true,正如我们在 IncludeInApiExplorerAttribute 类中实现的那样。

请参阅此帖子以获取对此的更多见解。

© www.soinside.com 2019 - 2024. All rights reserved.