【问题标题】:C# Attribute AppSettingsC# 属性 AppSettings
【发布时间】:2017-03-11 12:36:14
【问题描述】:

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

我有一个属性,我使用:[ApiExplorerSettings(IgnoreApi = true)] 向我的 Swagger 隐藏某些控制器。

这个属性是:System.Web.Http.Description的一部分

基本上我想在我的 web.config 文件中创建一个 AppSetting,所以当我发布到 Development 时,控制器会显示 (IgnoreApi = false),而当我发布到 Production 时,控制器会隐藏 (IgnoreApi = true)

我尝试直接在属性中访问ConfigurationManager.AppSettings,但这似乎无法按预期工作。

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

【问题讨论】:

  • 您可能必须创建一个派生属性,该属性基于一个常量值进行查找(请参阅here 示例),但 Swagger 可能会在忽略派生属性的同时寻找该确切属性.我猜只有一种方法可以找出答案:)

标签: c# asp.net .net api swashbuckle


【解决方案1】:

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

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

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 类中实现的那样。

请参阅此post 了解更多信息。

【讨论】:

    猜你喜欢
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 2016-11-04
    相关资源
    最近更新 更多