RunArg.cs 2.5 KB

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