public void ProcessRequest(HttpContext context) { if (_api == null) { context.Response.StatusCode = 404; return; } string action = (string)context.Items["action"]; bool queryUser = "******".Equals(action); context.Response.ContentEncoding = _encoding; context.Response.ContentType = "application/json"; var apiManager = ApiManager.GetOrCreate(_api.GetType()); if (!queryUser && !string.IsNullOrEmpty(action)) { apiManager.InvokeWebMethod(context, _api, action); } else { if (!ApiManager.IsDebug) { context.Response.StatusCode = 401; } else if (apiManager.EnableDocumentation()) { if (queryUser) { apiManager.InvokeReturnUser(context, _api.User); } else { if ("GET".Equals(context.Request.HttpMethod, StringComparison.CurrentCultureIgnoreCase)) { ResManager.ProcessRes(context, "documentation.html"); } else if ("POST".Equals(context.Request.HttpMethod, StringComparison.CurrentCultureIgnoreCase)) { context.Response.Write(JsonConvert.SerializeObject(apiManager.ApiDescriptor, Formatting.Indented)); } } } else { string returnString = "没有可用的文档,请定义Documentation特性。"; context.Response.Write(returnString); } } }
public static ApiManager GetOrCreate(Type type) { ApiManager manager; if (!_cache.TryGetValue(type, out manager)) { lock (_cache) { if (!_cache.TryGetValue(type, out manager)) { manager = new ApiManager(type); _cache.Add(type, manager); } } } return(manager); }
public void ProcessRequest(HttpContext context) { context.Response.ContentEncoding = _encoding; context.Response.ContentType = "text/html"; if (!ApiManager.IsDebug) { context.Response.StatusCode = 401; return; } if ("GET".Equals(context.Request.HttpMethod, StringComparison.CurrentCultureIgnoreCase)) { string path = context.Request.Path.TrimEnd('/'); var sb = new StringBuilder(); sb.Append("<html>"); sb.Append("<head>"); sb.Append("<title>WebApi beta 1.0</title>"); sb.AppendFormat("<link type='text/css' rel='stylesheet' href='{0}/special-res?res=main.css' />", path); sb.Append("</head>"); sb.Append("<body>"); sb.Append("<ul>"); foreach (Type type in _types) { sb.Append("<li>"); var apiManager = ApiManager.GetOrCreate(type); apiManager.FetchClassDocumentation(); var api = apiManager.ApiDescriptor; sb.AppendFormat("<div class='doc'>{0}</div>", api.Documentation); sb.AppendFormat("<div><a href='{0}' target='_blank'>{0}</a></div>", path + "/" + api.Name); sb.Append("</li>"); } sb.Append("</ul>"); sb.Append("</body>"); sb.Append("</html>"); context.Response.Write(sb.ToString()); } }
public static ApiManager GetOrCreate(Type type) { ApiManager manager; if (!_cache.TryGetValue(type, out manager)) { lock (_cache) { if (!_cache.TryGetValue(type, out manager)) { manager = new ApiManager(type); _cache.Add(type, manager); } } } return manager; }