public async Task InvokeAsync(HttpContext context) { //当一个请求为AJAX(Http请求头中X - Requested - With为XMLHttpRequest)时. //当客户端接受的返回类型为application / json(Http请求头中accept 为application / json)时. var xRequestedWith = context.Request.Headers["X-Requested-With"]; var accept = context.Request.Headers["accept"]; var isXMLHttpRequest = xRequestedWith.Any(t => t.Contains("XMLHttpRequest")); var isApplicationJson = accept.Any(t => t.Contains("application/json")); bool isJsonResult = isXMLHttpRequest || isApplicationJson; if (isJsonResult) { context.Response.StatusCode = StatusCodes.Status400BadRequest; var result = new Models.ResultModel() { Code = 1 }; var exceptionHandlerPathFeature = context.Features.Get <IExceptionHandlerPathFeature>(); if (exceptionHandlerPathFeature == null) { result.Message = "未知异常"; } else if (exceptionHandlerPathFeature.Error is DomainException domainException) { result.Message = domainException.Message; result.ValidationErrors = new List <Models.MemberError>(); result.ValidationErrors.Add(new Models.MemberError() { Member = domainException.Member, Message = domainException.Message }); } else { result.Message = exceptionHandlerPathFeature.Error.Message; } var options = new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var json = JsonSerializer.Serialize(result, options); await context.Response.WriteAsync(json); } else { context.Response.Redirect("/Home/Error"); } // Call the next delegate/middleware in the pipeline await _next(context); }
public ActionResult Calculator(string arg1, string arg2, string math) { Models.ResultModel model = new Models.ResultModel() { arg1 = arg1, arg2 = arg2, math = math, }; decimal a = 0; decimal b = 0; string result = ""; if (arg1 == "" || arg2 == "") { result = "Укажите, пожалуйста, оба аргумента"; } else if (!decimal.TryParse(arg1, out a) || !decimal.TryParse(arg2, out b)) { result = "При вводе аргументов воспользуйтесь, пожалуйста, клавишами с цифрами"; } else { switch (math) { case "+": result = (a + b).ToString(); break; case "-": result = (a - b).ToString(); break; case "*": result = (a * b).ToString(); break; case "/": result = b != 0 ? (a / b).ToString() : "На нуль делить нельзя"; break; default: result = "Выберите математическую функцию"; break; } } model.result = result; return(View(model)); }
/// <summary> /// Create a neuw ResultModel from a Marvel Character /// </summary> /// <param name="c">The character to use</param> /// <returns>A ResultModel with data from Marvel Character</returns> public static ResultModel FromMarvelToLocal(Characters c) { ResultModel rm = new Models.ResultModel(); rm.ID = c.id; rm.Name = c.name; rm.Avatar = c.Avatar; rm.TypeElement = ResultModel.Etype.Marvel; if (c.ComicLists != null) { rm.Badge = c.ComicLists.ComicSummaries.Count; } return(rm); }
public ActionResult Index(HttpPostedFileBase file) { Dictionary<string, char> morseCode = new Dictionary<string, char>() { ////Letters {".-", 'a'}, {"-...", 'b'}, {"-.-.", 'c'}, {"-..", 'd'}, {".", 'e'}, {"..-.", 'f'}, {"--.", 'g'}, {"....", 'h'}, {"..", 'i'}, {".---", 'j'}, {"-.-", 'k'}, {".-..", 'l'}, {"--", 'm'}, {"-.", 'n'}, {"---", 'o'}, {".--.", 'p'}, {"--.-", 'q'}, {".-.", 'r'}, {"...", 's'}, {"-", 't'}, {"..-", 'u'}, {"...-", 'v'}, {".--", 'w'}, {"-..-", 'x'}, {"-.--", 'y'}, {"--..", 'z'}, {" ", ' '}, ////Numbers {"-----", '0'}, {".----", '1'}, {"..----", '2'}, {"...--", '3'}, {"....-", '4'}, {".....", '5'}, {"-....", '6'}, {"--...", '7'}, {"---..", '8'}, {"----.", '9'}, }; string resulty = new StreamReader(file.InputStream).ReadToEnd(); // Verify that the user selected a file if (file != null && file.ContentLength > 0) { } var sb = new StringBuilder(); sb.Append(resulty); ////Removes breaks and blank lines string str = sb.Replace("||||", "|| ||") .Replace("\r\n", "||") .ToString(); char morseResult; ////Splits the string into an array based on the pipe breaks. string[] entryArray = Regex.Split(str, @"\|\|"); sb.Clear(); ////Rebuilds the string into deliverable format. foreach (var entry in entryArray) { if (morseCode.TryGetValue(entry, out morseResult)) { sb.Append(morseResult); } else { sb.Append("\n"); } } string result = sb.ToString(); MorseCodeMVC.Models.ResultModel resultModel = new Models.ResultModel(); resultModel.result = result; return View(resultModel); }