Search Results for

    Show / Hide Table of Contents

    Class BindingOptions

    request binding options

    Inheritance
    object
    BindingOptions
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: FastEndpoints
    Assembly: FastEndpoints.dll
    Syntax
    public sealed class BindingOptions

    Properties

    FailureMessage

    a function used to construct the failure message when a supplied value cannot be succesfully bound to a dto property during model binding.

    NOTE: this only applies to non-STJ operations. for customizing error messages of STJ binding failures, specify a JsonExceptionTransformer func.

    the following arguments are supplied to the function.

    Type: the type of the property which failed to bind

    string: the name of the property which failed to bind

    StringValues: the value that was attempted which resulted in the failure

    use these input parameters and construct your own error message string and return it from the function.
    Declaration
    public Func<Type, string, StringValues, string> FailureMessage { set; }
    Property Value
    Type Description
    Func<Type, string, StringValues, string>

    JsonExceptionTransformer

    by default, all STJ JsonExceptions thrown during deserialization are automatically caught and transformed using this function. if you'd like to disable this behavior, simply set this property to null or specify a function to construct a FluentValidation.Results.ValidationFailure when STJ throws an exception due to invalid json input.

    NOTE: this only applies to STJ based operations. for customizing error messages of non-STJ binding failures, specify a FailureMessage func.

    Declaration
    public Func<JsonException, ValidationFailure>? JsonExceptionTransformer { set; }
    Property Value
    Type Description
    Func<JsonException, ValidationFailure>

    Modifier

    an optional action to be run after the endpoint level request binding has occured. it is intended as a way to perform common model binding logic that applies to all endpoints/requests. the action is passed in the following arguments:

    object: the request dto instance

    Type: the type of the request dto

    BinderContext: the request binding context

    CancellationToken: a cancellation token

    WARNING: be mindful of the performance cost of using reflection to modify the request dto object

    Declaration
    public Action<object, Type, BinderContext, CancellationToken>? Modifier { set; }
    Property Value
    Type Description
    Action<object, Type, BinderContext, CancellationToken>

    Methods

    ValueParserFor(Type, Func<object?, ParseResult>)

    add a custom value parser function for any given type which the default model binder will use to parse values when model binding request dto properties from query/route/forms/headers/claims. this is an alternative approach to adding a TryParse() function to your types that need model binding support from the abovementioned binding sources. once you register a parser function here for a type, any TryParse() method on the type will not be used for parsing. also, these parser functions do not apply to JSON deserialization done by STJ and can be considered the equivalent to registering a custom converter in STJ when it comes to query/route/forms/headers/claims binding sources.

    Declaration
    public bool ValueParserFor(Type type, Func<object?, ParseResult> parser)
    Parameters
    Type Name Description
    Type type

    the type of the class which this parser function will target

    Func<object, ParseResult> parser

    a function that takes in a nullable object and returns a ParseResult as the output.

    app.UseFastEndpoints(c =>
    {
        c.Binding.ValueParserFor(typeof(Guid), MyParsers.GuidParser);
    });
    
    public static class MyParsers
    {
        public static ParseResult GuidParser(object? input)
        {
            Guid result;
            bool success = Guid.TryParse(input?.ToString(), out result);
            return new(success, result);
        }
    }
    Returns
    Type Description
    bool

    ValueParserFor<T>(Func<object?, ParseResult>)

    add a custom value parser function for any given type which the default model binder will use to parse values when model binding request dto properties from query/route/forms/headers/claims. this is an alternative approach to adding a TryParse() function to your types that need model binding support from the abovementioned binding sources. once you register a parser function here for a type, any TryParse() method on the type will not be used for parsing. also, these parser functions do not apply to JSON deserialization done by STJ and can be considered the equivalent to registering a custom converter in STJ when it comes to query/route/forms/headers/claims binding sources.

    Declaration
    public bool ValueParserFor<T>(Func<object?, ParseResult> parser)
    Parameters
    Type Name Description
    Func<object, ParseResult> parser

    a function that takes in a nullable object and returns a ParseResult as the output.

    app.UseFastEndpoints(c =>
    {
        c.Binding.ValueParserFor<Guid>(MyParsers.GuidParser);
    });
    
    public static class MyParsers
    {
        public static ParseResult GuidParser(object? input)
        {
            Guid result;
            bool success = Guid.TryParse(input?.ToString(), out result);
            return new(success, result);
        }
    }
    Returns
    Type Description
    bool
    Type Parameters
    Name Description
    T

    the type of the class which this parser function will target

    ValueParserWhen(Func<PropertyInfo, bool>, Func<object?, Type, ParseResult>)

    override value parsers for request dto properties that match a predicate.

    WARNING: might lead to weird/untraceable behavior. use at own risk!

    Declaration
    public void ValueParserWhen(Func<PropertyInfo, bool> propertyMatcher, Func<object?, Type, ParseResult> parser)
    Parameters
    Type Name Description
    Func<PropertyInfo, bool> propertyMatcher

    a predicate for qualifying a property

    Func<object, Type, ParseResult> parser

    the value parser for the matched property type

    In this article
    Back to top Developed by Đĵ ΝιΓΞΗΛψΚ and contributors / Licensed under MIT / Website generated by DocFX