DeviceBridge/Controllers/NotFoundResultFilterAttribute.cs (13 lines of code) (raw):
// Copyright (c) Microsoft Corporation. All rights reserved.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
/// <summary>
/// Converts a null return value into a 404.
/// </summary>
public class NotFoundResultFilterAttribute : ResultFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext context)
{
if (context.Result is ObjectResult objectResult && objectResult.Value == null)
{
context.Result = new NotFoundResult();
}
}
}