Пример #1
0
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            JimuModel model = null;
            var       req   = bindingContext.ActionContext.HttpContext.Request;

            if (req.HasFormContentType)
            {
                var form = req.Form;
                if (form != null && (form.Any() || form.Files.Any()))
                {
                    if (form.Files.Any())
                    {
                        var list = new List <JimuFile>();
                        foreach (var file in form.Files)
                        {
                            using (var sr = file.OpenReadStream())
                            {
                                var bytes = new byte[sr.Length];
                                sr.ReadAsync(bytes, 0, bytes.Length);
                                var myFile = new JimuFile
                                {
                                    FileName = file.FileName,
                                    Data     = bytes
                                };
                                list.Add(myFile);
                            }
                        }

                        var data = new Dictionary <string, object> {
                            { "files", list }
                        };
                        model = new JimuModel(data);
                    }
                    else
                    {
                        model = new JimuModel(form);
                    }
                }
            }
            else
            {
                var body = req.Body;
                if (body != null)
                {
                    try
                    {
                        model = new JimuModel(body, req.ContentType);
                    }
                    catch (Exception ex)
                    {
                        var logger = JimuClient.Host.Container.Resolve <ILogger>();
                        logger.Error("JimuModelBinder.BindModelAsync", ex);
                        throw;
                    }
                }
            }
            bindingContext.ModelState.SetModelValue("model", model, null);
            bindingContext.Result = ModelBindingResult.Success(model);
            return(Task.CompletedTask);
        }
Пример #2
0
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            JimuModel model = null;
            var       req   = bindingContext.ActionContext.HttpContext.Request;

            if (req.HasFormContentType)
            {
                var form = req.Form;
                if (form != null && (form.Any() || form.Files.Any()))
                {
                    if (form.Files.Any())
                    {
                        var list = new List <JimuFile>();
                        foreach (var file in form.Files)
                        {
                            using (var sr = file.OpenReadStream())
                            {
                                var bytes = new byte[sr.Length];
                                await sr.ReadAsync(bytes, 0, bytes.Length);

                                var myFile = new JimuFile
                                {
                                    FileName = file.FileName,
                                    Data     = bytes
                                };
                                list.Add(myFile);
                            }
                        }

                        var data = new Dictionary <string, object> {
                            { "files", list }
                        };
                        model = new JimuModel(data);
                    }
                    else
                    {
                        model = new JimuModel(form);
                    }
                }
            }
            else
            {
                var body = req.Body;
                if (body != null)
                {
                    model = new JimuModel();
                    await model.ReadFromContentAsync(body, req.ContentType);
                }
            }
            bindingContext.ModelState.SetModelValue("model", model, null);
            bindingContext.Result = ModelBindingResult.Success(model);
        }
Пример #3
0
        //public async Task<object> Path(string path, [FromQuery] MyQueryString query, [FromBody] Dictionary<string, object> model)
        public async Task <IActionResult> JimuPath(string path, [FromQuery] JimuQueryString query, [ModelBinder] JimuModel model)
        {
            var paras = new Dictionary <string, object>();

            if (model?.Data != null)
            {
                paras = model.Data;
            }
            if (query.Collection.Count > 0)
            {
                foreach (var key in query.Collection.Keys)
                {
                    paras[key] = query.Collection[key];
                }
            }
            if (!path.StartsWith("/"))
            {
                path = "/" + path;
            }
            var result = await JimuClient.Invoke($"{path}{(query.Query ?? "")}", paras, Request.Method);

            if (result?.ResultType != null && result.ResultType.StartsWith("{\"ReturnType\":\"Jimu.JimuRedirect\""))
            {
                var redirect = result.Result as JimuRedirect;
                return(Redirect(redirect.RedirectUrl));
            }
            //if (result.ResultType != typeof(JimuFile).ToString())
            if (result?.ResultType != null && result.ResultType.StartsWith("{\"ReturnType\":\"Jimu.JimuFile\""))
            {
                var file = result.Result as JimuFile;
                return(File(file?.Data, "application/octet-stream", file?.FileName));
            }
            //return new JsonResult(result.Result);
            return(Ok(result.Result));
        }