protected async override Task RunAsync(HttpApiContext context) { string prefix = context.RoutePrefix; string url = context.Request.RawUrl.Substring(prefix.Length); if (url.StartsWith("/")) { url = url.Substring(1); } string[] tokens = url.Split('/'); string methodName = tokens[0]; var method = this.GetType().GetMethod(methodName); List<object> plist = new List<object>(); foreach (var p in method.GetParameters()) { plist.Add(Load(context,p)); } var to = method.Invoke(this, plist.ToArray()); var tobj = to as Task; if (tobj != null) { await tobj; var p = tobj.GetType().GetProperty("Result"); context.Result = p.GetValue(tobj); } else { context.Result = to; } }
public async Task<HttpApiResponse> ExecuteAsync(HttpApiRequest request) { try { var url = request.RawUrl; var context = new HttpApiContext { Request = request }; var r = RegisteredRoutes.FirstOrDefault(x => url.StartsWith(x.RoutePrefix, StringComparison.OrdinalIgnoreCase)); if (r == null) { using (var c = new HttpProxyController()) { await c.ExecuteAsync(context); return context.Response; } } using (BaseHttpApiController c = Activator.CreateInstance(r.Controller) as BaseHttpApiController) { context.RoutePrefix = r.RoutePrefix; await c.ExecuteAsync(context); return context.Response; } } catch (Exception ex) { return HttpApiResponse.FromException(ex); } }
protected override async Task RunAsync(HttpApiContext context) { using (WebRequestHandler handler = new WebRequestHandler()) { handler.UseCookies = false; handler.AllowAutoRedirect = false; handler.ServerCertificateValidationCallback = delegate { return true; }; //handler.ClientCertificateOptions = ClientCertificateOption.Automatic; handler.AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip; using (HttpClient client = new HttpClient(handler)) { HttpRequestMessage request = Create(context.Request); var response = await client.SendAsync(request); var data = await response.Content.ReadAsByteArrayAsync(); context.Response = SetResponse(response, data); var ct = response.Content.Headers.ContentType; if (ct != null) { context.Response.ContentType = ct.ToString(); } //context.Response.ContentType = response.Content.Headers.ContentType.ToString(); } } }
protected void PostProcess(HttpApiContext context) { if (context.Response == null) { context.Response = context.Result as HttpApiResponse; if (context.Response == null) { context.Response = HttpApiResponse.ToJson(context.Result); } } }
private object Load(HttpApiContext context, ParameterInfo p) { string key = p.Name; var source = ModelSource.FormOrQuery; var ms = p.GetCustomAttribute<ModelSourceAttribute>(); if (ms != null) { source = ms.ModelSource; if (!string.IsNullOrWhiteSpace(ms.Name)) { key = ms.Name; } } var pt = p.ParameterType; object value = LoadValue(context, source, key, pt) ?? p.DefaultValue; if (value == null) { if (pt.IsValueType) return Activator.CreateInstance(pt); if (pt == typeof(string)) return value; if (pt.GetGenericTypeDefinition() == typeof(Nullable<>)) return value; } /// load model from form.. if (source == ModelSource.FormOrQuery) { } else { var model = context.Request.Form["formModel"]; return JsonConvert.DeserializeObject(model, pt); } return value; }
private object LoadValue(HttpApiContext context, ModelSource source, string key, Type type) { object v = null; switch (source) { case ModelSource.Header: return context.Request.Headers[key]; case ModelSource.FormOrQuery: v = context.Request.Form[key]; if (v == null) { v = context.Request.QueryString[key]; } return v; case ModelSource.FormJson: return JsonConvert.DeserializeObject(context.Request.TextContent, type); case ModelSource.QueryString: return context.Request.QueryString[key]; case ModelSource.QueryStringJson: v = context.Request.QueryString[key]; if (v != null) { v = JsonConvert.DeserializeObject((string)v, type); } return v; case ModelSource.FormModelJson: break; default: break; } return null; }
protected virtual void Init(HttpApiContext context) { }
protected abstract Task RunAsync(HttpApiContext context);
public async Task ExecuteAsync(HttpApiContext context) { Init(context); await RunAsync(context); PostProcess(context); }