/// <summary> /// Applies the alternate web root path to the given address, replacing a leading "~" with the configuration BaseUrl. /// </summary> /// <param name="address">The address onto which to apply the alternate web root.</param> /// <param name="config">The <see cref="AlternateWebRootConfiguration"/> to use.</param> /// <returns> /// If the address starts with a "~" character, the address is returned with the "~" character replaced /// with the alternate web root; otherwise, the original address. /// </returns> public static string Apply(string address, AlternateWebRootConfiguration config = null) { if (string.IsNullOrEmpty(address)) { throw new ArgumentNullException(nameof(address)); } if (config == null) { config = AlternateWebRootConfiguration.Global; } if (address.StartsWith("~", StringComparison.Ordinal)) { address = address.Substring(1); address = config.AddressPrefix + address; } else if (config.IsIncludingSiteRelativePaths && address.StartsWith("/", StringComparison.Ordinal) && !address.StartsWith("//", StringComparison.Ordinal)) { address = config.AddressPrefix + address; } return(address); }
/// <summary> /// Initializes the AlternateWebRoot as being enabled globally for use in the application. /// </summary> /// <param name="appBuilder">The current <see cref="IApplicationBuilder"/> being configured.</param> /// <param name="baseUrl">The <see cref="Uri"/> to use as the alternative for web root relative paths.</param> /// <returns>The original <see cref="IApplicationBuilder"/> passed in.</returns> public static IApplicationBuilder UseAlternateWebRoot(this IApplicationBuilder appBuilder, Uri baseUrl) { var config = new AlternateWebRootConfiguration { BaseUrl = baseUrl, }; return(UseAlternateWebRoot(appBuilder, config)); }
/// <summary> /// Initializes the AlternateWebRoot for use in the application. /// </summary> /// <param name="appBuilder">The current <see cref="IApplicationBuilder"/> being configured.</param> /// <param name="altConfig">The <see cref="AlternateWebRootConfiguration"/> to use globally.</param> /// <returns>The original <see cref="IApplicationBuilder"/> passed in.</returns> public static IApplicationBuilder UseAlternateWebRoot(this IApplicationBuilder appBuilder, AlternateWebRootConfiguration altConfig = null) { if (appBuilder == null) { throw new ArgumentNullException(nameof(appBuilder)); } if (altConfig == null) { var configuration = (IConfiguration)appBuilder.ApplicationServices.GetService(typeof(IConfiguration)); altConfig = configuration?.GetSection("AlternateWebRoot")?.Get <AlternateWebRootConfiguration>(); } AlternateWebRootConfiguration.Global = altConfig ?? new AlternateWebRootConfiguration(); return(appBuilder); }