zoukankan      html  css  js  c++  java
  • WebApi 整合 Swagger 和 MiniProfiler

    参考文章:https://www.cnblogs.com/jiujiduilie/p/8371378.html

    1、通过NuGet安装MiniProfiler

    2、通过Nuget安装MiniProfiler.EF6

    3、通过Nuget安装Swashbuckle

     4、添加web.config配置,添加到<handlers>节点下面

    <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" 
    type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" 
    preCondition="integratedMode" />

    5、在 Global.asax中添加

    protected void Application_BeginRequest()
    {
        MiniProfiler.Start();
    }
    protected void Application_EndRequest()
    {
        MiniProfiler.Stop();
    }

    6、添加InjectMiniProfiler类,然后在App_Start/SwaggerConfig.cs中的EnableSwagger中添加 c.DocumentFilter<InjectMiniProfiler>();

    public class InjectMiniProfiler : IDocumentFilter
     {
         public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
         {
             swaggerDoc.info.contact = new Contact()
             {
                 name = MiniProfiler.RenderIncludes().ToHtmlString()
             };
         }
     }

    7、添加SwaggerUiCustomization.js,并且右键该文件修改其属性中的【生成操作】为【嵌入的资源】

    //Create a mini profiler script tag with the right properites 
    var MiniProfiler = $('#api_info > div:nth-child(3)').text();
    const attributes = [
        'src', 'data-version', 'data-path', 'data-current-id', 'data-ids',
        'data-position', 'data-trivial', 'data-children', 'data-max-traces', 'data-controls',
        'data-authorized', 'data-toggle-shortcut', 'data-start-hidden', 'data-trivial-milliseconds'
    ];
    var GetAttr = function (input, attributeName) {
        const myRegexp = attributeName + '="(.*?)"';
        const re = new RegExp(myRegexp, "g");
        const match = re.exec(input);
        return match[1];
    }
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.id = "mini-profiler";
    s.async = true;
    for (var i = 0; i < attributes.length; i++) {
        var element = attributes[i];
        s.setAttribute(element, GetAttr(MiniProfiler, element));
    }
    document.body.appendChild(s);
    // Remove injected tag from view 
    $('#api_info > div:nth-child(3)').text('');

    8、给swaggerconfig.cs添加

    string resourceName2 = thisAssembly.FullName.Substring(0, thisAssembly.FullName.IndexOf(",")) + ".Scripts.swaggerui.SwaggerUiCustomization.js";
     c.InjectJavaScript(thisAssembly, resourceName2);

    9、添加WebApiProfilingActionFilter类

    public class WebApiProfilingActionFilter : ActionFilterAttribute
        {
            public const string MiniProfilerResultsHeaderName = "X-MiniProfiler-Ids";
    
            public override void OnActionExecuted(HttpActionExecutedContext filterContext)
            {
                var MiniProfilerJson = JsonConvert.SerializeObject(new[] {MiniProfiler.Current.Id});
                filterContext.Response.Content.Headers.Add(MiniProfilerResultsHeaderName, MiniProfilerJson);
            }
        }

    10、往SwaggerUiCustomization.js文件中添加代码window.angular=true;

    11、在WebApiConfig.cs中注册这个Filterconfig.Filters.Add(new WebApiProfilingActionFilter());

    12、将【项目】-【属性】-【生成】-【输出】-XML文档文件 勾上

     GitHub:https://github.com/zhuanshujianghai/WebApi_Swagger_Miniprofiler_Demo

  • 相关阅读:
    vs2008.net多语言实现方法
    C#中 Process的扩展类ProcessExtensions
    C#获取当前系统信息的类
    非常好用的GridView控件yyControls中的SmartGridView
    Android提供两个常用的消息弹出框【Toast和Alert】
    [置顶] Asp.net中实现多语言的Page的扩展的基类
    C# word类库
    在系统出现未处理的错误时,在Global的Application_Error记录下错误
    向大家推荐一个非常好用的JS日历控件My97DatePicker
    网页代码测试工具集合
  • 原文地址:https://www.cnblogs.com/jianghaidong/p/9837563.html
Copyright © 2011-2022 走看看