2017-11-06 1 views
0

새로운 .Net 핵심 웹 API에 Swashbuckle을 추가하여 Swagger 설명서를 사용하도록 설정했습니다.실제로 변경하지 않고 시도해보십시오.

제공된 Try-It-Out 옵션을 실제로 파헤 치십시오.

그러나 밑줄 친 비즈니스 로직을 호출하기 전에 라이브러리에서 확인할 수있는 항목이 있습니까?

예를 들면. 삭제 끝점에서 시험해보십시오. swagger가 전화를 걸기 위해 사용 된 이벤트에서 리소스를 실제로 삭제하고 싶지는 않지만 버튼을 그대로두고 작업하고 싶습니다.

+0

당신이 할 수 있습니까? ... 가능한 경우 InjectOnCompleteJavaScript를 사용하여 JS를 주입 할 수 있습니다. https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/98f64b09032c2949e669c319b5cf09ff0f4e2340/src/Swashbuckle.AspNetCore.SwaggerUI/Application/SwaggerUIOptions.cs#L89 – HelderSepu

+0

[Swashbuckle] (https://github.com/domaindrivendev/Swashbuckle) 또는 [Swashbuckle.AspNetCore] (https://github.com/domaindrivendev/Swashbuckle.AspNetCore)를 사용 하시겠습니까? 그리고 어떤 버전? – Helen

+1

@Helen - Swashbuckle.AspNetCore 사용, v 1.0.0. 특정 헤더 값을 제공하는 자바 스크립트를 삽입하고 콘트롤러에서 볼 수 있습니다. – Darren

답변

0

나는 Swagger UI를 그대로 두는 아이디어를 좋아하지만 임의의 데이터를 임의로 커밋 할 수있는 아이디어는 아닙니다.

사례를 처리 할 수있는 간단한 ActionFilterAttribute을 만들기로 결정했습니다.

원래 게시물에서와 같이 아이디어는 버튼이 작동하도록 허용했지만 일부 동사의 경우 실제로는 데이터를 변경하지 않습니다.

그래서 내 해상도는 이것이다 :

사용자 지정 작업 필터 특성 :

/// <summary> 
/// Simple action filter to prevent executing the action for certain http methods 
/// </summary> 
public class SwaggerIgnore : ActionFilterAttribute 
{ 
    string[] _ignoreMethods; 

    /// <summary> 
    /// Ignores the request when coming from Swagger. 
    /// 
    /// Will return a default OK result. 
    /// 
    /// Handy if you want to allow the "try" button, but not allow changes to entities; such as HttpDelete 
    /// </summary> 
    /// <param name="ignoreMethods">List of methods to ignore; get, delete etc</param> 
    public SwaggerIgnore(string[] ignoreMethods) 
    { 
     _ignoreMethods = ignoreMethods.Select(s => s.ToLower()).ToArray(); 
    } 


    public override void OnActionExecuting(ActionExecutingContext context) 
    { 
     string methodExecuting = context.HttpContext.Request.Method.ToLower(); 
     string referrer = context.HttpContext.Request.Headers["Referer"].ToString().ToLower(); 
     if (_ignoreMethods.Contains(methodExecuting) && referrer.Contains("swagger")) 
      context.Result = new OkResult(); 
     else 
      base.OnActionExecuting(context); 
    } 
} 

그리고 Startup.cs 파일 내에서

은, 그래서 설정 :

services.AddMvc(options => 
    { 
     options.Filters.Add(new SwaggerIgnore(new[] { "delete", "post", "put" })); 
    }); 

을 이제, 하나를 실행할 때 삭제, 게시 또는 Swagger 페이지에서 가져온 데이터는 실제로 처리되지 않지만 작업은 OK를 반환합니다.

0

대안으로는 약간의 유연성을 허용하는 AuthorizeAttribute을 사용하는 것이 좋습니다. 다만 일부는 허용하지만 다른 일부는 허용하지 않을 수 있습니다.

public class IgnoreSwaggerUiRequestAttribute : AuthorizeAttribute 
{ 
    protected override void HandleUnauthorizedRequest(HttpActionContext a) 
    { 
     bool methodNotAllowed = false; 
     try 
     { 
      methodNotAllowed = a.Request.Headers.Referrer.AbsoluteUri.Contains("swagger/ui"); 
     } 
     catch { } 
     if (methodNotAllowed) 
     { 
      a.Response = new HttpResponseMessage 
      { 
       StatusCode = HttpStatusCode.MethodNotAllowed, 
       Content = new StringContent("This method is not allowed from the swagger ui") 
      }; 
     } 
    } 
} 

그리고는 우리는 우리는이 같은 데이터를 표시하지 않는 방법을 장식 : 자바 스크립트로 무엇을 요구

[IgnoreSwaggerUiRequest] 
    public string Delete(int id) 
관련 문제