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 successfully 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>

    FormExceptionTransformer

    if this function is specified, any internal exceptions that are thrown by asp.net when accessing multipart form data will be caught and transformed to validation failures using this function. by default those exceptions are not caught and thrown out to the middleware pipeline. setting this func might come in handy if you need 413 responses (that arise from incoming request body size exceeding kestrel's MaxRequestBodySize) automatically transformed to 400 problem details responses.

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

    JsonExceptionStatusCode

    this http status code will be used for all automatically sent JsonException responses which are built using the JsonExceptionTransformer func. defaults to 400.

    Declaration
    public int JsonExceptionStatusCode { set; }
    Property Value
    Type Description
    int

    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>

    ReflectionCache

    the central cache of request dto related reflection data. populating this cache with source generated data will eliminate expression compilations during runtime as well as usage of reflection based property setters, etc. see the source generator documentation on how to populate this cache with generated data.

    Declaration
    public ReflectionCache ReflectionCache { get; }
    Property Value
    Type Description
    ReflectionCache

    UsePropertyNamingPolicy

    specify whether to use the json property naming policy when matching incoming field names to dto property names for non-json model binding. only applies when field names are not specified on properties with attributes such as [BindFrom(...)], [FromClaim(...)], [FromHeader(...)] etc.

    Declaration
    public bool UsePropertyNamingPolicy { get; set; }
    Property Value
    Type Description
    bool

    Methods

    ValueParserFor(Type, Func<StringValues, 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 void ValueParserFor(Type type, Func<StringValues, ParseResult> parser)
    Parameters
    Type Name Description
    Type type

    the type of the class which this parser function will target

    Func<StringValues, 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);
        }
    }

    ValueParserFor<T>(Func<StringValues, 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 void ValueParserFor<T>(Func<StringValues, ParseResult> parser)
    Parameters
    Type Name Description
    Func<StringValues, 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);
        }
    }
    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