您可以通过注入来实现这一点IHttpContextAccessor https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.ihttpcontextaccessor?view=aspnetcore-3.1控制器中的接口。
该接口提供了访问 HttpContext 对象的抽象,主要是在 Web 库之外。
要使用它,你必须services.AddHttpContextAccessor();
在您的启动文件中。
然后你可以将它注入到你的控制器中。
[ApiController]
public class ErrorController
{
private readonly ILogger<ErrorController> logger;
private readonly IHttpContextAccessor httpContextAccessor;
public ErrorController(ILogger<ErrorController> logger, IHttpContextAccessor httpContextAccessor){
this.logger = logger;
this.httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
}
}
然后在您的方法中,您使用访问器接口中的 HttpContext 而不是基类。
[Route("/error-development")]
public IActionResult HandleErrorDevelopment()
{
var exceptionFeature = this.httpContextAccessor.HttpContext.Features.Get<IExceptionHandlerFeature>();
var error = exceptionFeature.Error;
}
由于我们现在正在针对注入的接口进行工作,因此您可以在单元测试中模拟它。请记住,您必须模拟您正在调用的整个链。 (我在本例中使用 xUnit 和 Moq)。
public ErrorControllerTests() {
this.httpContextAccessorMock = new Mock<IHttpContextAccessor>();
this.httpContextMock = new Mock<HttpContext>();
this.featureCollectionMock = new Mock<IFeatureCollection>();
this.exceptionHandlerFeatureMock = new Mock<IExceptionHandlerFeature>();
this.httpContextAccessorMock.Setup(ca => ca.HttpContext).Returns(this.httpContextMock.Object);
this.httpContextMock.Setup(c => c.Features).Returns(this.featureCollectionMock.Object);
this.featureCollectionMock.Setup(fc => fc.Get<IExceptionHandlerFeature>()).Returns(this.exceptionHandlerFeatureMock.Object);
this.controller = new ErrorController(null, this.httpContextAccessorMock.Object);
}
[Fact]
public void HandleErrorDevelopment() {
Exception thrownException;
try{
throw new ApplicationException("A thrown application exception");
}
catch(Exception ex){
thrownException = ex;
}
this.exceptionHandlerFeatureMock.Setup(ehf => ehf.Error).Returns(thrownException);
var result = this.controller.HandleErrorDevelopment();
var objectResult = result as ObjectResult;
Assert.NotNull(objectResult);
Assert.Equal(500, objectResult.StatusCode);
var problem = objectResult.Value as ProblemDetails;
Assert.NotNull(problem);
Assert.Equal(thrownException.Message, problem.Title);
Assert.Equal(thrownException.StackTrace, problem.Detail);
}
重要的: 您不能使用ControllerBase.Problem()
方法。该实现依赖于ControllerBase.HttpContext
检索一个ProblemDetailsFactory
object.
您可以在源代码 https://github.com/dotnet/aspnetcore/blob/master/src/Mvc/Mvc.Core/src/ControllerBase.cs。由于您的单元测试没有设置上下文,因此它会抛出NullReferenceException
.
Factory只不过是创建一个的帮手ProblemDetails https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.problemdetails?view=aspnetcore-3.1目的。再次你可以看到GitHub 上的默认实现 https://github.com/dotnet/aspnetcore/blob/master/src/Mvc/Mvc.Core/src/Infrastructure/DefaultProblemDetailsFactory.cs.
我最终创建了自己的私有方法来创建这个对象。
private ProblemDetails CreateProblemDetails(
int? statusCode = null,
string title = null,
string type = null,
string detail = null,
string instance = null){
statusCode ??= 500;
var problemDetails = new ProblemDetails
{
Status = statusCode.Value,
Title = title,
Type = type,
Detail = detail,
Instance = instance,
};
var traceId = Activity.Current?.Id ?? this.httpContextAccessor.HttpContext?.TraceIdentifier;
if (traceId != null)
{
problemDetails.Extensions["traceId"] = traceId;
}
return problemDetails;
}
完整的错误处理方法如下所示。
[Route("/error-development")]
public IActionResult HandleErrorDevelopment() {
var exceptionFeature = this.httpContextAccessor.HttpContext.Features.Get<IExceptionHandlerFeature>();
var error = exceptionFeature.Error;
this.logger?.LogError(error, "Unhandled exception");
var problem = this.CreateProblemDetails(title: error.Message, detail: error.StackTrace);
return new ObjectResult(problem){
StatusCode = problem.Status
};
}
对于生产,我要么提供要向用户显示的通用消息,要么在已经有要显示的用户友好消息的代码中抛出自定义处理的异常。
if (error is HandledApplicationException)
{
// handled exceptions with user-friendly message.
problem = this.CreateProblemDetails(title: error.Message);
}
else {
// unhandled exceptions. Provice a generic error message to display to the end user.
problem = this.CreateProblemDetails(title: "An unexpected exception occured. Please try again or contact IT support.");
}