您可以通过在控制器中注入IHttpContextAccessor 接口来实现此目的。
这个接口提供了一个抽象来访问 HttpContext 对象,主要是在你的网络库之外。
要使用它,你必须在你的启动文件中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 对象。
您可以在source code 中看到它。由于您的单元测试没有设置上下文,它会抛出一个NullReferenceException。
Factory 只不过是创建ProblemDetails 对象的助手。你可以再次看到Default implementation on GitHub。
我最终创建了自己的私有方法来创建这个对象。
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.");
}