要在.NET Core项目中集成MiniProfiler和Swagger,可按照以下步骤操作:
可选
)。 <PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.3.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
services.AddMiniProfiler(options =>
{
options.RouteBasePath = "/profiler";
})
.AddEntityFramework();
app.UseMiniProfiler();
app.UseSwaggerUI(c =>
{
c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("YourNamespace.index.html");
c.RoutePrefix = string.Empty;
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
YourNamespace
为你的项目命名空间。[HttpGet]
public IActionResult GetCounts()
{
var html = MiniProfiler.Current.RenderIncludes(_accessor.HttpContext);
return Ok(html.Value);
}
public IEnumerable<WeatherForecast> GetAll2()
{
using (MiniProfiler.Current.Step("获取成功后,开始处理最终数据"))
{
// 一些逻辑...
using (MiniProfiler.Current.Step("准备数据"))
{
using (MiniProfiler.Current.CustomTiming("SQL", "SELECT * FROM Config"))
{
// 模拟一个SQL查询
Thread.Sleep(500);
}
}
}
var datas = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
});
return datas;
}
会出现流访问异常,就是不能再开个swagger进行访问 An unhandled exception occurred while processing the request. ArgumentException: Stream was not readable. System.IO.StreamReader..ctor(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen)
app.UseSwaggerUI(c => {
c.InjectJavascript("/custom.js");
}
然后再wwwroot文件夹下创建 custom.js
,将生成的Javascript进行更改即可
custom.js
// 等待 DOM 完全加载
document.addEventListener('DOMContentLoaded', function () {
// 创建一个新的 script 元素
var newScript = document.createElement('script');
// 设置 script 的属性
newScript.async = true; // 设置为异步加载
newScript.id = 'mini-profiler'; // 设置 ID
newScript.src = '/profiler/includes.min.js?v=4.3.8+1120572909'; // 设置脚本的源文件路径
newScript.setAttribute('data-version', '4.3.8+1120572909');
newScript.setAttribute('data-path', '/profiler/');
newScript.setAttribute('data-current-id', '551f7bde-3d0b-4fe1-8cef-c6945f6f4d58');
newScript.setAttribute('data-ids', 'a264a19a-395d-4e61-970f-6249ab868614,d26da3fb-eca3-4ada-899b-e85058c6010b,8dc76f68-5c1c-495d-95d0-5f07258aacf1,70f16caa-76de-4cd0-a957-82849d471053,41628017-8871-4b2a-af0c-5dfc2a6424cd,51df7af6-93ee-44b1-ba70-97920acbd3b9,4cee7860-8154-4897-81d7-7436c7408778,ba92e686-e4e3-4af6-8329-3c14645998b8,dbe17478-119b-49e3-bd4b-a83fe182354d,551f7bde-3d0b-4fe1-8cef-c6945f6f4d58');
newScript.setAttribute('data-position', 'Left');
newScript.setAttribute('data-scheme', 'Light');
newScript.setAttribute('data-authorized', 'true');
newScript.setAttribute('data-max-traces', '15');
newScript.setAttribute('data-toggle-shortcut', 'Alt+P');
newScript.setAttribute('data-trivial-milliseconds', '2.0');
newScript.setAttribute('data-ignored-duplicate-execute-types', 'Open,OpenAsync,Close,CloseAsync');
// 将 script 元素添加到文档的 head 中
document.head.appendChild(newScript);
});
注意事项:
https://gitee.com/Pridejoy/PublicCode/tree/master/MiniProfilerSwaggerDemo