Skip to content

Commit

Permalink
Fix NotSupportedException in AoT project
Browse files Browse the repository at this point in the history
Fix `NotSupportedException` being thrown with .NET 9
 when accessing `ModelMetadata.ElementType` in a native AoT application due to `Microsoft.AspNetCore.Mvc.ApiExplorer.IsEnhancedModelMetadataSupported` being disabled.
  • Loading branch information
martincostello committed Nov 23, 2024
1 parent 00890c9 commit 09561bb
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,17 @@ internal static bool IsFromBody(this ApiParameterDescription apiParameter)

internal static bool IsFromForm(this ApiParameterDescription apiParameter)
{
bool isEnhancedModelMetadataSupported = true;

#if NET9_0_OR_GREATER
if (AppContext.TryGetSwitch("Microsoft.AspNetCore.Mvc.ApiExplorer.IsEnhancedModelMetadataSupported", out var isEnabled))
{
isEnhancedModelMetadataSupported = isEnabled;
}
#endif

var source = apiParameter.Source;
var elementType = apiParameter.ModelMetadata?.ElementType;
var elementType = isEnhancedModelMetadataSupported ? apiParameter.ModelMetadata?.ElementType : null;

return (source == BindingSource.Form || source == BindingSource.FormFile)
|| (elementType != null && typeof(IFormFile).IsAssignableFrom(elementType));
Expand Down

0 comments on commit 09561bb

Please sign in to comment.