2016-10-28 1 views
2

속성 내가 사용하는 속성이 있습니다 [ApiExplorerSettings(IgnoreApi = true)]을 내 자신감의 특정 컨트롤러를 숨길 수 있습니다. System.Web.Http.DescriptionC#을 내가 .NET 웹 API (4.6 프레임 워크)</p> <p>를 사용하여 응용 프로그램을 쓰고 있어요 appSettings는

은 기본적으로 내가 컨트롤러가 (IgnoreApi = false) 표시, 개발에 게시 그렇게 할 때 내 web.config 파일에 AppSetting을 만들려면 내가 생산에 게시, 컨트롤러 :

이 속성의 일부입니다 숨겨진 (IgnoreApi = true)입니다.

속성에서 직접 ConfigurationManager.AppSettings에 액세스를 시도했지만 예상대로 작동하지 않는 것 같습니다.

아마도 IgnoreApi의 getter/setter에서 내 web.config에서 올바른 값을 가져올 수 있도록 해당 속성을 재정의 할 방법을 찾아야합니까?

+0

상수 값을 기반으로 조회를 수행하는 파생 된 특성을 만들어야 할 수도 있습니다 ([here] (http://stackoverflow.com/questions/6665187/how-to-set-dynamic-value- in-my-attribute) 예를 들어, Swagger는 파생 된 속성을 무시하면서 정확한 속성을 찾고있을 수 있습니다. 알아낼 수있는 유일한 방법은 다음과 같습니다. – Stijn

+0

[속성의 설정 사용] (http://stackoverflow.com/questions/18619184/using-config-settings-in-attributes)의 가능한 복제본 –

답변

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; } } 
} 
  • 그런 다음 우리는 다음과 같이 정의 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을 참조하십시오.

관련 문제