C#属性设置
我正在使用.NETWebAPI(4.6框架)编写一个应用程序 我使用了一个属性:C#属性设置,c#,asp.net,.net,api,swashbuckle,C#,Asp.net,.net,Api,Swashbuckle,我正在使用.NETWebAPI(4.6框架)编写一个应用程序 我使用了一个属性:[ApiExplorerSettings(IgnoreApi=true)]来隐藏某些控制器,以免招摇过市 此属性是:System.Web.Http.Description 基本上我想在我的web.config文件中创建一个AppSetting,这样当我发布到开发时,控制器会显示(IgnoreApi=false),当我发布到生产时,控制器会隐藏(IgnoreApi=true) 我已尝试直接在属性中访问Configura
[ApiExplorerSettings(IgnoreApi=true)]
来隐藏某些控制器,以免招摇过市
此属性是:System.Web.Http.Description
基本上我想在我的web.config文件中创建一个AppSetting,这样当我发布到开发时,控制器会显示(IgnoreApi=false)
,当我发布到生产时,控制器会隐藏(IgnoreApi=true)
我已尝试直接在属性中访问ConfigurationManager.AppSettings
,但这似乎没有达到预期效果
也许我需要找到一种方法来覆盖该属性,以便在IgnoreApi的getter/setter上,它可以从我的web.config中提取正确的值?扩展“ApiExplorerSettingsAttribute”类似乎很简单,但它是密封的。因此,最终采用了以下解决方法:
- 从基础calss“属性”继承的自定义属性
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; } }
}
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;
}
}
- 然后我们可以实现一个定制的ApiExplorer,如下所示
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; } }
}
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;
}
}
- 然后在控制器或操作中,可以添加自定义
属性如下所示
IsInApi是我们可以设置为true或false的web.config值。若并没有设置,那个么它将默认设置为true,正如我们在IncludeInApiExplorerAttribute类中实现的那个样[IncludeInApiExplorer("IsInAPI")]
请参阅此部分以了解更多信息。您可能必须创建一个基于常量值进行查找的派生属性(请参见示例),但Swagger可能会在忽略派生属性的同时查找该确切属性。我想只有一种方法可以找到:)可能是