Exemplo n.º 1
0
        private static bool TrySetPropertyFromRequestUrl(PropertyBindingContext pbc, RequestBindingContext context)
        {
            if (!context.Request.RouteValues.TryGetValue(pbc.Name, out var paramValue))
            {
                return(false);
            }

            var propertyValue = Convert.ChangeType(paramValue, pbc.CommandProperty.PropertyType);

            return(context.TrySetCommandProperty(pbc, propertyValue));
        }
Exemplo n.º 2
0
        private static bool TrySetPropertyFromForm(PropertyBindingContext pbc, RequestBindingContext context)
        {
            var formValues = context.FormData[pbc.Name];

            if (formValues.Count < 1)
            {
                return(false);
            }

            return(context.TrySetCommandProperty(pbc, formValues[0]));
        }
Exemplo n.º 3
0
            public bool TrySetCommandProperty(PropertyBindingContext pbc, object value)
            {
                if (value == null)
                {
                    return(false);
                }

                try
                {
                    pbc.CommandProperty.SetValue(Command, value);
                    return(true);
                }
                catch
                {
                    // maybe log?
                    return(false);
                }
            }
Exemplo n.º 4
0
        private static void TrySetProperty(PropertyBindingContext bi, RequestBindingContext context)
        {
            // Try from the request body first
            if (TrySetPropertyFromRequestBody(bi, context))
            {
                return;
            }

            if (TrySetPropertyFromRequestUrl(bi, context))
            {
                return;
            }

            if (TrySetPropertyFromQueryString(bi, context))
            {
                return;
            }

            TrySetPropertyFromForm(bi, context);
        }
Exemplo n.º 5
0
        private static bool TrySetPropertyFromRequestBody(PropertyBindingContext pbc, RequestBindingContext context)
        {
            var bodyProperty = context.BodyObject?.Property(pbc.Name, StringComparison.OrdinalIgnoreCase);

            if (bodyProperty == null)
            {
                return(false);
            }

            var commandPropertyType = pbc.CommandProperty.PropertyType;

            object propertyValue;

            try
            {
                propertyValue = bodyProperty.Value.ToObject(commandPropertyType);
            }
            catch
            {
                return(false);
            }

            return(context.TrySetCommandProperty(pbc, propertyValue));
        }
Exemplo n.º 6
0
 public CommandBindingContext(bool directFromBody) : this()
 {
     DirectFromBody = directFromBody;
     Properties     = new PropertyBindingContext[0];
 }