나는 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를 반환합니다.
당신이 할 수 있습니까? ... 가능한 경우 InjectOnCompleteJavaScript를 사용하여 JS를 주입 할 수 있습니다. https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/98f64b09032c2949e669c319b5cf09ff0f4e2340/src/Swashbuckle.AspNetCore.SwaggerUI/Application/SwaggerUIOptions.cs#L89 – HelderSepu
[Swashbuckle] (https://github.com/domaindrivendev/Swashbuckle) 또는 [Swashbuckle.AspNetCore] (https://github.com/domaindrivendev/Swashbuckle.AspNetCore)를 사용 하시겠습니까? 그리고 어떤 버전? – Helen
@Helen - Swashbuckle.AspNetCore 사용, v 1.0.0. 특정 헤더 값을 제공하는 자바 스크립트를 삽입하고 콘트롤러에서 볼 수 있습니다. – Darren