Beispiel #1
0
        protected ActionResult Result(ActionOptions options, object model = null, JsonSerializerSettings settings = null)
        {
            if (settings == null)
            {
                settings = new JsonSerializerSettings();
            }

            if (_bootOptions.IsCamelCasePropertyNames)
            {
                settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            }


            if (!options.ResultType.HasValue)
            {
                options.ResultType = ResultType.Json;
            }

            switch (options.ResultType)
            {
            case ResultType.Json:
                Response.ContentType = "application/json; charset=utf-8";

                if (model is IEnumerable data)
                {
                    var total = HttpContext.Items["Total"];
                    if (total == null)
                    {
                        return(Content(JsonConvert.SerializeObject(new Response(data), settings)));
                    }

                    var resultDto = new ResultDto
                    {
                        Data  = data,
                        Total = (int)total
                    };
                    return(Content(JsonConvert.SerializeObject(new Response(resultDto), settings)));
                }
                else
                {
                    if (model == null)
                    {
                        return(Content(JsonConvert.SerializeObject(new Response())));
                    }
                    return(Content(JsonConvert.SerializeObject(new Response(model), settings)));
                }

            case ResultType.View:
                return(View(model));
            }

            return(null);
        }
Beispiel #2
0
 protected IActionResult SafeExecute(Func <IActionResult> doing, ActionOptions actionOptions)
 {
     try
     {
         return(doing());
     }
     catch (InputException e)
     {
         HttpContext.Items["InputException"] = true;
         HttpContext.Items["Error"]          = e;
         HttpContext.Items["actionOptions"]  = actionOptions;
         throw;
     }
 }