-
Notifications
You must be signed in to change notification settings - Fork 8
/
AuthorEntityBinderProvider.cs
38 lines (33 loc) · 1.47 KB
/
AuthorEntityBinderProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright © Benjamin Abt 2020-2024, all rights reserved
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
namespace BenjaminAbt.HCaptcha.AspNetCore;
/// <summary>
/// Provides a model binder for binding an <see cref="HCaptchaVerifyResponse"/> to an action parameter.
/// </summary>
public class AuthorEntityBinderProvider : IModelBinderProvider
{
/// <summary>
/// Returns a model binder for the <see cref="HCaptchaVerifyResponse"/> model type, if applicable.
/// </summary>
/// <param name="context">The context that provides information about the model being bound.</param>
/// <returns>
/// An <see cref="IModelBinder"/> instance if the model type is <see cref="HCaptchaVerifyResponse"/>, otherwise <c>null</c>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when the <paramref name="context"/> is <c>null</c>.
/// </exception>
public IModelBinder? GetBinder(ModelBinderProviderContext context)
{
// Ensure the context is not null
ArgumentNullException.ThrowIfNull(context);
// Check if the model type is HCaptchaVerifyResponse
if (context.Metadata.ModelType == typeof(HCaptchaVerifyResponse))
{
// Return a binder for HCaptchaVerifyResponse
return new BinderTypeModelBinder(typeof(HCaptchaModelBinder));
}
// Return null if no binder is required for the model type
return null;
}
}