"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;
}
}
이것은 우리의 사용자 지정 특성에 의해 판독 된 값입니다. WebAPI.config.cs 파일에서 web.config 파일
<appSettings>
<add key="IsInAPI" value="false"/>
</appSettings>
가 우리가 대체 한 IApiExplorer를 추가합니다.
config.Services.Replace(typeof(IApiExplorer), new OptApiExplorer(config));
는 그런 다음 컨트롤러 나 액션에서 당신은 정의를 추가 할 수 있습니다 속성을 다음과 같이. [IncludeInApiExplorer("IsInAPI")]
IsInApi 우리가 true 또는 false로 설정할 수 있습니다 Web.config의 값입니다. 설정되어 있지 않으면 IncludeInApiExplorerAttribute 클래스에서 구현 한대로 기본값이 true로 설정됩니다.
자세한 내용은 post을 참조하십시오.
상수 값을 기반으로 조회를 수행하는 파생 된 특성을 만들어야 할 수도 있습니다 ([here] (http://stackoverflow.com/questions/6665187/how-to-set-dynamic-value- in-my-attribute) 예를 들어, Swagger는 파생 된 속성을 무시하면서 정확한 속성을 찾고있을 수 있습니다. 알아낼 수있는 유일한 방법은 다음과 같습니다. – Stijn
[속성의 설정 사용] (http://stackoverflow.com/questions/18619184/using-config-settings-in-attributes)의 가능한 복제본 –