public static string GetRequestBody(HttpContextBase http, Type requestType, ContentType requestContentType) { Guard.NotNull(http, "http"); Guard.NotNull(requestType, "requestType"); Guard.NotNull(requestContentType, "requestContentType"); string body = null; if (http.Request != null && http.Request.InputStream != null) { using (var stream = http.Request.InputStream) { using (var reader = new StreamReader(stream, true)) { body = reader.ReadToEnd(); } } } if (string.IsNullOrWhiteSpace(body)) { return requestContentType.GetDefaultBody(requestType); } else { return body; } }
public object CreateRequest(IContainer container, HttpContextBase http, ContentType requestContentType) { Guard.NotNull(container, "container"); Guard.NotNull(http, "http"); Guard.NotNull(requestContentType, "requestContentType"); var match = Result.Pattern.Match(http.Request.Path); if (!match.Success) { throw new InvalidOperationException(Messages.RequestNotMatched); } var body = GetRequestBody(http, RequestType, requestContentType); var serializer = requestContentType.CreateSerializer(); var request = serializer.Deserialize(RequestType, body); for (int i = 1; i < match.Groups.Count; i++) { var groupValue = match.Groups[i].Value; var property = Result.RouteGroups[i]; var convertedValue = Convert(property.PropertyType, groupValue); property.SetValue(request, convertedValue); } return request; }