[英]C# Attribute AppSettings
我正在使用.NET Web API(4.6框架)编写应用程序
我有一个使用的属性: [ApiExplorerSettings(IgnoreApi = true)]
从我的Swagger隐藏某些控制器。
此属性是: System.Web.Http.Description
的一部分
基本上,我想在我的web.config文件中创建一个AppSetting,以便当我发布到Development时,控制器显示出来(IgnoreApi = false)
而当我发布到Production时,控制器被隐藏起来(IgnoreApi = true)
。
我尝试直接在属性中访问ConfigurationManager.AppSettings
,但这似乎无法按预期工作。
也许我需要找到一种方法来覆盖该属性,以便在IgnoreApi的getter / setter上,它可以从我的web.config中提取正确的值?
扩展“ 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; } }
}
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>
在那里,我们用自定义类替换了IApiExplorer。
config.Services.Replace(typeof(IApiExplorer), new OptApiExplorer(config));
然后,可以在Controller或Action中添加如下所示的custom属性。
[IncludeInApiExplorer("IsInAPI")]
IsInApi是web.config值,可以将其设置为true或false。 如果未设置,则将默认设置为true,这是我们在IncludeInApiExplorerAttribute类中实现的。
请参阅此帖子以获取有关此内容的更多见解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.