PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Photopia.Web/Controllers/HomeController.cs

https://github.com/ServicesLab/Photopia
C# | 83 lines | 63 code | 19 blank | 1 comment | 0 complexity | 9fcfd399676f1c8922bd956aac8ff20c MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Web.Http;
  4. using System.Web.Http.Description;
  5. using System.Web.Mvc;
  6. using System.Xml.Linq;
  7. namespace Photopia.Web.Controllers
  8. {
  9. public class ApiListViewModel
  10. {
  11. public string Name { get; set; }
  12. public IEnumerable<ApiDescription> ApiDescriptions { get; set; }
  13. }
  14. public class ApiDocumentation
  15. {
  16. public string Description { get; set; }
  17. public string JsonResponse { get; set; }
  18. public string HttpMethod { get; set; }
  19. public string Url { get; set; }
  20. }
  21. public class ApiParameter
  22. {
  23. public string Name { get; set; }
  24. public string Description { get; set; }
  25. public string Example { get; set; }
  26. }
  27. public class HomeController : Controller
  28. {
  29. public ActionResult Index()
  30. {
  31. XDocument doc = XDocument.Load(Server.MapPath("~/App_Data/ApiDoc.xml"));
  32. IEnumerable<XElement> memberElements = doc.Descendants("members").Descendants("member")
  33. .Where(x => x.Attribute("name").Value.StartsWith("M:") &&
  34. x.Attribute("name").Value.ToLowerInvariant().Contains("api")).ToList();
  35. IApiExplorer apiExplorer = GlobalConfiguration.Configuration.Services.GetApiExplorer();
  36. var groupedApiDescriptions = from a in apiExplorer.ApiDescriptions
  37. group a by a.ActionDescriptor.ControllerDescriptor.ControllerName
  38. into g
  39. select new ApiListViewModel {Name = g.Key, ApiDescriptions = g};
  40. foreach (var member in memberElements)
  41. {
  42. var vv = member.Attribute("name").Value;
  43. string ss = "fs";
  44. }
  45. foreach (var g in groupedApiDescriptions)
  46. {
  47. foreach (var desc in g.ApiDescriptions)
  48. {
  49. var actionName = ((System.Web.Http.Controllers.ReflectedHttpActionDescriptor)(desc.ActionDescriptor)).MethodInfo.Name;
  50. var fullName = desc.ActionDescriptor.ControllerDescriptor.ControllerType.FullName;
  51. string selector = string.Format("M:{0}.{1}", fullName, actionName).ToLower();
  52. XElement xElement = memberElements.Where(x => x.Attribute("name").Value.ToLower().Contains(selector)).FirstOrDefault();
  53. }
  54. }
  55. return View(groupedApiDescriptions.ToList());
  56. //http://blogs.msdn.com/b/yaohuang1/archive/2012/05/21/asp-net-web-api-generating-a-web-api-help-page-using-apiexplorer.aspx
  57. return View();
  58. }
  59. public ActionResult UploadImage()
  60. {
  61. return View();
  62. }
  63. }
  64. }