我正在使用.NET Web API(4.6 Framework)编写应用程序
我使用了一个属性:[ApiExplorerSettings(IgnoreApi = true)]
来隐藏Swagger中的某些控制器。
此属性属于:System.Web.Http.Description
基本上我想在我的web.config文件中创建一个AppSetting,所以当我发布到Development时,控制器显示(IgnoreApi = false)
,当我发布到Production时,控制器被隐藏(IgnoreApi = true)
。< / p>
我已尝试直接在属性中访问ConfigurationManager.AppSettings
,但这似乎无法按预期工作。
也许我需要找到一种覆盖该属性的方法,以便在IgnoreApi的getter / setter上,它可以从我的web.config中提取正确的值?
答案 0 :(得分:0)
扩展“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中添加自定义 属性如下。
[IncludeInApiExplorer("IsInAPI")]
IsInApi 是我们可以设置为true或false的web.config值。如果未设置,则默认设置为true,因为我们已在IncludeInApiExplorerAttribute类中实现。
请参阅此post以获取更多相关信息。