You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.9 KiB

4 years ago
  1. using Apewer.Web;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. namespace Apewer.AspNetBridge
  7. {
  8. /// <summary></summary>
  9. public abstract class ApiController
  10. {
  11. /// <summary></summary>
  12. public ApiRequest Request { get; internal set; }
  13. /// <summary></summary>
  14. public ApiResponse Response { get; internal set; }
  15. /// <summary></summary>
  16. protected virtual IHttpActionResult Bytes(byte[] bytes, string contentType = "application/octet-stream", string attachmentName = null)
  17. {
  18. var har = new HttpActionResult();
  19. har.Bytes = bytes;
  20. har.ContentType = contentType;
  21. har.Attachment = attachmentName;
  22. return har;
  23. }
  24. /// <summary></summary>
  25. protected virtual IHttpActionResult Text(string text, string contentType = "text/plain")
  26. {
  27. var har = new HttpActionResult();
  28. har.Bytes = text.Bytes();
  29. har.ContentType = contentType;
  30. return har;
  31. }
  32. /// <summary></summary>
  33. protected virtual IHttpActionResult Json<T>(T content)
  34. {
  35. var json = Apewer.Json.From(content, false, -1, true);
  36. var text = json == null ? "" : json.ToString();
  37. return Bytes(text.Bytes(), "application/json");
  38. }
  39. /// <summary></summary>
  40. protected static string MapPath(string relativePath)
  41. {
  42. var root = RuntimeUtility.ApplicationPath;
  43. var path = root;
  44. if (relativePath.NotEmpty())
  45. {
  46. var split = relativePath.Split('/');
  47. foreach (var seg in split)
  48. {
  49. if (seg.IsEmpty()) continue;
  50. if (seg == "~") path = root;
  51. path = Path.Combine(path, seg);
  52. }
  53. }
  54. return path;
  55. }
  56. }
  57. }