Git Product home page Git Product logo

Comments (9)

abatishchev avatar abatishchev commented on September 27, 2024

I see inconsistence: you're using [MvcStyleBinding] or [MvcActionBinding] or both ?

from webapicontrib.

allnnde avatar allnnde commented on September 27, 2024

in the IHttpActionResult ? i'm use [MvcStyleBinding]

from webapicontrib.

abatishchev avatar abatishchev commented on September 27, 2024

But the error occurs in private class MvcActionBinding?

from webapicontrib.

allnnde avatar allnnde commented on September 27, 2024

Yes, the complete class is:

public class MvcActionValueBinder : DefaultActionValueBinder
 {
        // Per-request storage, uses the Request.Properties bag. We need a unique key into the bag.
        private const string Key = "{5DC187FB-BFA0-462A-AB93-9E8036871EC8}";

        public override HttpActionBinding GetBinding(HttpActionDescriptor actionDescriptor)
        {
            var actionBinding = new MvcActionBinding();

            HttpParameterDescriptor[] parameters = actionDescriptor.GetParameters().ToArray();
            HttpParameterBinding[] binders = Array.ConvertAll(parameters, DetermineBinding);

            actionBinding.ParameterBindings = binders;

            return actionBinding;
        }

        private HttpParameterBinding DetermineBinding(HttpParameterDescriptor parameter)
        {
            HttpConfiguration config = parameter.Configuration;
            var attr = new ModelBinderAttribute(); // use default settings

            ModelBinderProvider provider = attr.GetModelBinderProvider(config);
            IModelBinder binder = provider.GetBinder(config, parameter.ParameterType);

            // Alternatively, we could put this ValueProviderFactory in the global config.
            var vpfs = new List<ValueProviderFactory>(attr.GetValueProviderFactories(config)) { new BodyValueProviderFactory() };
            return new ModelBinderParameterBinding(parameter, binder, vpfs);
        }

        // Derive from ActionBinding so that we have a chance to read the body once and then share that with all the parameters.
        private class MvcActionBinding : HttpActionBinding
        {
            // Read the body upfront , add as a ValueProvider
            public override Task ExecuteBindingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
            {
                HttpRequestMessage request = actionContext.ControllerContext.Request;
                HttpContent content = request.Content;
                if (content != null)
                {
                    FormDataCollection fd = content.ReadAsAsync<FormDataCollection>().Result;
                    if (fd != null)
                    {
                        IValueProvider vp = new NameValuePairsValueProvider(fd, CultureInfo.InvariantCulture);
                        request.Properties.Add(Key, vp);
                    }
                }

                return base.ExecuteBindingAsync(actionContext, cancellationToken);
            }
        }

        // Get a value provider over the body. This can be shared by all parameters.
        // This gets the values computed in MvcActionBinding.
        private class BodyValueProviderFactory : ValueProviderFactory
        {
            public override IValueProvider GetValueProvider(HttpActionContext actionContext)
            {
                object vp;
                actionContext.Request.Properties.TryGetValue(Key, out vp);
                return (IValueProvider)vp; // can be null 
            }
        }
    }

from webapicontrib.

allnnde avatar allnnde commented on September 27, 2024

hello, i'm chage

FormDataCollection fd = content.ReadAsAsync<FormDataCollection>().Result;

for

var fd = content.ReadAsAsync<IDictionary<string, object>>().Result;

and it work but doesn't when send array :(

from webapicontrib.

abatishchev avatar abatishchev commented on September 27, 2024

btw don't use Result, use async/await instead

from webapicontrib.

allnnde avatar allnnde commented on September 27, 2024

when? in the content.ReadAsAsync<IDictionary<string, object>>().Result?

from webapicontrib.

abatishchev avatar abatishchev commented on September 27, 2024

It's unrelated to the problem you're facing but yes, there and elsewhere, calling the Result property blocks current thread so you're not having async operation spite it looks like you will. If shortly, this is incorrect way to use it.

from webapicontrib.

abatishchev avatar abatishchev commented on September 27, 2024

Regarding your problem, i frankly don't know, I'm not familiar with this part of the codebase. Please ask a question on Stack Overflow and provide a link to the question here, I'll assist if you're facing any issues there

from webapicontrib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.