RunArg.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using System.IO;
  6. using Vit.Extensions;
  7. namespace Vit.WebHost
  8. {
  9. public class RunArg
  10. {
  11. public Func<IWebHostBuilder> OnCreateWebHostBuilder = () => new WebHostBuilder().UseKestrel();
  12. /// <summary>
  13. /// 是否异步运行.Runs a web application and returns a Task that only completes when the token is triggered or shutdown is triggered.
  14. /// </summary>
  15. public bool RunAsync = false;
  16. #region wwwrootPath
  17. /// <summary>
  18. /// 静态文件路径。若不指定(null)则不映射静态文件
  19. /// </summary>
  20. private string _wwwrootPath = null;
  21. /// <summary>
  22. /// 静态文件路径。可为相对路径或绝对路径。若为空字符串则默认为当前目录下的wwwroot文件夹。若不指定(null)则不映射静态文件。
  23. /// </summary>
  24. public string wwwrootPath
  25. {
  26. get => _wwwrootPath;
  27. set
  28. {
  29. #region (x.1) get fullPath
  30. string fullPath = value;
  31. if ("" == fullPath)
  32. {
  33. fullPath = Path.Combine(AppContext.BaseDirectory, "wwwroot");
  34. }
  35. else if (fullPath != null)
  36. {
  37. if (!Directory.Exists(fullPath))
  38. {
  39. fullPath = Path.Combine(AppContext.BaseDirectory, fullPath);
  40. }
  41. }
  42. if (string.IsNullOrEmpty(fullPath))
  43. {
  44. fullPath = null;
  45. }
  46. else
  47. {
  48. var dir = new DirectoryInfo(fullPath);
  49. if (dir.Exists)
  50. {
  51. fullPath = dir.FullName;
  52. }
  53. else
  54. {
  55. fullPath = null;
  56. }
  57. }
  58. #endregion
  59. _wwwrootPath = fullPath;
  60. }
  61. }
  62. #endregion
  63. /// <summary>
  64. /// 初始化 wwwroot静态文件配置的操作
  65. /// </summary>
  66. public Action<StaticFileOptions> OnInitStaticFileOptions;
  67. public Action<IServiceCollection> OnConfigureServices = null;
  68. public Action<IApplicationBuilder> OnConfigure = null;
  69. public string[] urls;
  70. /// <summary>
  71. /// 允许跨域访问
  72. /// </summary>
  73. public bool allowAnyOrigin = false;
  74. public RunArg SetPort(int port = 8888)
  75. {
  76. urls = new string[] { "http://*:" + port };
  77. return this;
  78. }
  79. }
  80. }