Source/NFX.Wave/Handlers/MVCHandler.cs
/// <summary>
/// Fills method invocation param array with args doing some interpretation for widely used types like JSONDataMaps, Rows etc..
/// </summary>
Fills method invocation param array with args doing some interpretation for widely used types like JSONDataMaps, Rows etc
Fills method invocation param array with args doing some interpretation for widely used types like JSONDataMaps Rows etc
protected virtual void BindParameters(Controller controller, string action, ActionAttribute attrAction, MethodInfo method,  WorkContext work, out object[] args)
      {
        var mpars = method.GetParameters();
        args = new object[mpars.Length];
        if (mpars.Length==0) return;
        var requested = work.WholeRequestAsJSONDataMap;
        var strictParamBinding = attrAction.StrictParamBinding;
        for(var i=0; i<mpars.Length; i++)
        {
          var ctp = mpars[i].ParameterType;
          if (ctp==typeof(object) || ctp==typeof(JSONDataMap) || ctp==typeof(Dictionary<string, object>))
          {
            args[i] = requested;
            continue;
          }
          if (typeof(TypedRow).IsAssignableFrom(ctp))
          {
            try
            {
              args[i] = JSONReader.ToRow(ctp, requested);
              continue;
            }
            catch(Exception error)
            {
              throw new HTTPStatusException(WebConsts.STATUS_400,
                                            WebConsts.STATUS_400_DESCRIPTION,
                                            error.ToMessageWithType(),
                                            error);
            }
          }
        }
        for(var i=0; i<args.Length; i++)
        {
          if (args[i]!=null) continue;
          var mp = mpars[i];
          var got = requested[mp.Name];
          if (got==null)
          {
            if (mp.HasDefaultValue) args[i] = mp.DefaultValue;
            continue;
          }
          if (got is byte[])
          {
            if (mp.ParameterType==typeof(byte[]))
            {
              args[i] = got;
              continue;
            }
            if (mp.ParameterType==typeof(Stream) || mp.ParameterType==typeof(MemoryStream))
            {
              args[i] = new MemoryStream((byte[])got, false);
              continue;
            }
            if (strictParamBinding)
             throw new HTTPStatusException(Web.WebConsts.STATUS_400,
                                        Web.WebConsts.STATUS_400_DESCRIPTION,
                                        StringConsts.MVC_CONTROLLER_ACTION_PARAM_BINDER_ERROR
                                                    .Args(
                                                          controller.GetType().DisplayNameWithExpandedGenericArgs(),
                                                          "strict",
                                                          action,
                                                          mp.Name,
                                                          mp.ParameterType.DisplayNameWithExpandedGenericArgs(), "byte[]" ));
          }
          var strVal = got.AsString();
          try
          {
            args[i] = strVal.AsType(mp.ParameterType, strictParamBinding);
          }
          catch
          {
            const int MAX_LEN = 30;
            if (strVal.Length>MAX_LEN) strVal = strVal.Substring(0, MAX_LEN)+"...";
            throw new HTTPStatusException(WebConsts.STATUS_400,
                                         WebConsts.STATUS_400_DESCRIPTION,
                                        StringConsts.MVC_CONTROLLER_ACTION_PARAM_BINDER_ERROR
                                                    .Args(
                                                          controller.GetType().DisplayNameWithExpandedGenericArgs(),
                                                          strictParamBinding ? "strict" : "relaxed",
                                                          action,
                                                          mp.Name,
                                                          mp.ParameterType.DisplayNameWithExpandedGenericArgs(), strVal ));
          }
        }
      }
protected virtual void BindParameters ( Controller controller , string action , ActionAttribute attrAction , MethodInfo method , WorkContext work , out object [ ] args ) { var mpars = method . GetParameters ( ) ; args = new object [ mpars . Length ] ; if ( mpars . Length == 0 ) return ; var requested = work . WholeRequestAsJSONDataMap ; var strictParamBinding = attrAction . StrictParamBinding ; for ( var i = 0 ; i < mpars . Length ; i ++ ) { var ctp = mpars [ i ] . ParameterType ; if ( ctp == typeof ( object ) || ctp == typeof ( JSONDataMap ) || ctp == typeof ( Dictionary < string , object > ) ) { args [ i ] = requested ; continue ; } if ( typeof ( TypedRow ) . IsAssignableFrom ( ctp ) ) { try { args [ i ] = JSONReader . ToRow ( ctp , requested ) ; continue ; } catch ( Exception error ) { throw new HTTPStatusException ( WebConsts . STATUS_400 , WebConsts . STATUS_400_DESCRIPTION , error . ToMessageWithType ( ) , error ) ; } } } for ( var i = 0 ; i < args . Length ; i ++ ) { if ( args [ i ] != null ) continue ; var mp = mpars [ i ] ; var got = requested [ mp . Name ] ; if ( got == null ) { if ( mp . HasDefaultValue ) args [ i ] = mp . DefaultValue ; continue ; } if ( got is byte [ ] ) { if ( mp . ParameterType == typeof ( byte [ ] ) ) { args [ i ] = got ; continue ; } if ( mp . ParameterType == typeof ( Stream ) || mp . ParameterType == typeof ( MemoryStream ) ) { args [ i ] = new MemoryStream ( ( byte [ ] ) got , false ) ; continue ; } if ( strictParamBinding ) throw new HTTPStatusException ( Web . WebConsts . STATUS_400 , Web . WebConsts . STATUS_400_DESCRIPTION , StringConsts . MVC_CONTROLLER_ACTION_PARAM_BINDER_ERROR . Args ( controller . GetType ( ) . DisplayNameWithExpandedGenericArgs ( ) , " " , action , mp . Name , mp . ParameterType . DisplayNameWithExpandedGenericArgs ( ) , " " ) ) ; } var strVal = got . AsString ( ) ; try { args [ i ] = strVal . AsType ( mp . ParameterType , strictParamBinding ) ; } catch { const int MAX_LEN = 30 ; if ( strVal . Length > MAX_LEN ) strVal = strVal . Substring ( 0 , MAX_LEN ) + " " ; throw new HTTPStatusException ( WebConsts . STATUS_400 , WebConsts . STATUS_400_DESCRIPTION , StringConsts . MVC_CONTROLLER_ACTION_PARAM_BINDER_ERROR . Args ( controller . GetType ( ) . DisplayNameWithExpandedGenericArgs ( ) , strictParamBinding ? " " : " " , action , mp . Name , mp . ParameterType . DisplayNameWithExpandedGenericArgs ( ) , strVal ) ) ; } } }
Fills method invocation param array with args doing some interpretation for widely used types like JSONDataMaps, Rows etc..
Fills method invocation param array with args doing some interpretation for widely used types like JSONDataMaps Rows etc ..
//check for complex type //got byte[]
controller
Controller
action
string
attrAction
ActionAttribute
method
MethodInfo
work
WorkContext
object[]
out