public async Task <RequestModel> ParseRequestData() { RequestModel model = null; if (httpContext.Request.Method.ToLower() == "post") { var contentType = String.Empty; if (httpContext.Request.Headers.ContainsKey("Content-Type")) { contentType = httpContext.Request.Headers["Content-Type"].ToString(); } var data = String.Empty; if (contentType.ToLower().Contains("multipart/form-data")) { model = new RequestModel(); var formData = httpContext.Request.Form; foreach (var item in formData) { model.Add(item.Key, item.Value); } } if (contentType.ToLower() == "application/json") { using (var reader = new StreamReader(httpContext.Request.Body)) { data = await reader.ReadToEndAsync(); reader.Close(); } if (!String.IsNullOrWhiteSpace(data)) { var jsonSerializerOptions = new JsonSerializerOptions { AllowTrailingCommas = true, IgnoreNullValues = true, PropertyNameCaseInsensitive = true, MaxDepth = 2, }; jsonSerializerOptions.Converters.Add(new HttpService.Serializer.RequestModelJsonConverter()); model = JsonSerializer.Deserialize <RequestModel>(data, jsonSerializerOptions); } } } if (httpContext.Request.Method.ToLower() == "get") { throw new NotImplementedException(); } return(model); }
public ActionResult SendRequestHandler(FormCollection form) { string username = HttpContext.Request.Cookies["login"]["name"]; string classId = form["classId"].Trim(); string content = form["content"].Trim(); Request request = new Request() { ClassId = classId, Content = content, StudentUsername = username, }; RequestModel requestModel = new RequestModel(); requestModel.Add(request); return(RedirectToAction("Index")); }