【发布时间】:2021-03-23 07:40:11
【问题描述】:
我已经有一段时间没有开发 API 了,所以请耐心等待。我在新的 .NET 5.0 框架中创建了一个新的 Web API。我尝试使用 Hellang.Middleware.ProblemDetails nuget 来处理我的错误处理中间件。似乎正在工作,但我无法获得任何堆栈跟踪详细信息来终生显示我,我是否缺少某些东西?
我只能得到以下详细信息:
{"type":"https://httpstatuses.com/404","title":"不是 找到","状态":404,"traceId":"00-02c4e89a990c5745bc4250cfad83d5e3-bb8c1dab98b44a44-00"}
这是我的启动类中的相关代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<CoreDbContext>(op => op.UseSqlServer(AppSettings.DBConnectionString).UseLazyLoadingProxies());
services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddProblemDetails(opts =>
{
// Control when an exception is included
opts.IncludeExceptionDetails = (ctx, ex) =>
{
// Fetch services from HttpContext.RequestServices
var env = ctx.RequestServices.GetRequiredService<IHostEnvironment>();
return env.IsDevelopment();
};
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseProblemDetails();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
【问题讨论】:
-
您能否提供一些有关您尝试发出的请求以及您的控制器和路由代码的信息?不会有任何带有 404 的异常消息,因为它只是一个标准响应类型。如果您想测试获取堆栈跟踪详细信息,请尝试在控制器代码中故意抛出异常并查看响应中发送的内容。
标签: c# .net error-handling .net-5