WebUrlServiceBase.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Abp.Extensions;
  2. using VberZero.Configuration;
  3. namespace VberZero.DomainService.Url;
  4. public abstract class WebUrlServiceBase
  5. {
  6. public const string TenancyNamePlaceHolder = "{TENANCY_NAME}";
  7. public abstract string WebSiteRootAddressFormatKey { get; }
  8. public abstract string ServerRootAddressFormatKey { get; }
  9. public string WebSiteRootAddressFormat => _appConfiguration[WebSiteRootAddressFormatKey] ?? "https://localhost:62114/";
  10. public string ServerRootAddressFormat => _appConfiguration[ServerRootAddressFormatKey] ?? "https://localhost:62114/";
  11. public bool SupportsTenancyNameInUrl
  12. {
  13. get
  14. {
  15. var siteRootFormat = WebSiteRootAddressFormat;
  16. return !siteRootFormat.IsNullOrEmpty() && siteRootFormat.Contains(TenancyNamePlaceHolder);
  17. }
  18. }
  19. private readonly IConfigurationRoot _appConfiguration;
  20. protected WebUrlServiceBase(IAppConfigurationAccessor configurationAccessor)
  21. {
  22. _appConfiguration = configurationAccessor.Configuration;
  23. }
  24. public string GetSiteRootAddress(string tenancyName = null)
  25. {
  26. return ReplaceTenancyNameInUrl(WebSiteRootAddressFormat, tenancyName);
  27. }
  28. public string GetServerRootAddress(string tenancyName = null)
  29. {
  30. return ReplaceTenancyNameInUrl(ServerRootAddressFormat, tenancyName);
  31. }
  32. public List<string> GetRedirectAllowedExternalWebSites()
  33. {
  34. var values = _appConfiguration["App:RedirectAllowedExternalWebSites"];
  35. return values?.Split(',').ToList() ?? new List<string>();
  36. }
  37. private string ReplaceTenancyNameInUrl(string siteRootFormat, string tenancyName)
  38. {
  39. if (!siteRootFormat.Contains(TenancyNamePlaceHolder))
  40. {
  41. return siteRootFormat;
  42. }
  43. if (siteRootFormat.Contains(TenancyNamePlaceHolder + "."))
  44. {
  45. siteRootFormat = siteRootFormat.Replace(TenancyNamePlaceHolder + ".", TenancyNamePlaceHolder);
  46. }
  47. if (tenancyName.IsNullOrEmpty())
  48. {
  49. return siteRootFormat.Replace(TenancyNamePlaceHolder, "");
  50. }
  51. return siteRootFormat.Replace(TenancyNamePlaceHolder, tenancyName + ".");
  52. }
  53. }