diff --git a/src/Web/Pages/Index.cshtml b/src/Web/Pages/Index.cshtml index e90def0c..cc07e690 100644 --- a/src/Web/Pages/Index.cshtml +++ b/src/Web/Pages/Index.cshtml @@ -5,7 +5,9 @@ }
- + + +
@@ -39,7 +41,7 @@ else {
- THERE ARE NO RESULTS THAT MATCH YOUR SEARCH + @Model.SettingsModel.NoResultsMessage
} diff --git a/src/Web/Pages/Index.cshtml.cs b/src/Web/Pages/Index.cshtml.cs index 87697916..1fe38b97 100644 --- a/src/Web/Pages/Index.cshtml.cs +++ b/src/Web/Pages/Index.cshtml.cs @@ -1,16 +1,19 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.eShopWeb.Web.Services; using Microsoft.eShopWeb.Web.ViewModels; +using Microsoft.Extensions.Options; namespace Microsoft.eShopWeb.Web.Pages; public class IndexModel : PageModel { private readonly ICatalogViewModelService _catalogViewModelService; + public SettingsViewModel SettingsModel { get; } - public IndexModel(ICatalogViewModelService catalogViewModelService) + public IndexModel(ICatalogViewModelService catalogViewModelService, IOptionsSnapshot options) { _catalogViewModelService = catalogViewModelService; + SettingsModel = options.Value; } public CatalogIndexViewModel CatalogModel { get; set; } = new CatalogIndexViewModel(); diff --git a/src/Web/Pages/SettingsViewModel.cs b/src/Web/Pages/SettingsViewModel.cs new file mode 100644 index 00000000..5dbf52df --- /dev/null +++ b/src/Web/Pages/SettingsViewModel.cs @@ -0,0 +1,6 @@ +namespace Microsoft.eShopWeb.Web.Pages; + +public class SettingsViewModel +{ + public string NoResultsMessage { get; set; } +} diff --git a/src/Web/Pages/_ViewImports.cshtml b/src/Web/Pages/_ViewImports.cshtml index 2bf31ee4..d85ca5f5 100644 --- a/src/Web/Pages/_ViewImports.cshtml +++ b/src/Web/Pages/_ViewImports.cshtml @@ -7,3 +7,4 @@ @using Microsoft.eShopWeb.Infrastructure.Identity @namespace Microsoft.eShopWeb.Web.Pages @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@addTagHelper *, Microsoft.FeatureManagement.AspNetCore diff --git a/src/Web/Program.cs b/src/Web/Program.cs index e1181e2c..c0aead2b 100644 --- a/src/Web/Program.cs +++ b/src/Web/Program.cs @@ -16,6 +16,10 @@ using Microsoft.eShopWeb.Web.Configuration; using Microsoft.eShopWeb.Web.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks; +using Azure.Identity; +using Microsoft.eShopWeb.Web.Pages; +using Microsoft.FeatureManagement; +using Microsoft.IdentityModel.Tokens; var builder = WebApplication.CreateBuilder(args); @@ -74,6 +78,33 @@ config.Path = "/allservices"; }); +// Bind configuration "eShopWeb:Settings" section to the Settings object +builder.Services.Configure(builder.Configuration.GetSection("eShopWeb:Settings")); +// Initialize useAppConfig parameter +var useAppConfig = false; +Boolean.TryParse(builder.Configuration["UseAppConfig"], out useAppConfig); +// Add Azure App Configuration middleware to the container of services. +builder.Services.AddAzureAppConfiguration(); +builder.Services.AddFeatureManagement(); +// Load configuration from Azure App Configuration +if (useAppConfig) +{ + builder.Configuration.AddAzureAppConfiguration(options => + { + options.Connect(new Uri(builder.Configuration["AppConfigEndpoint"]), new DefaultAzureCredential()) + .ConfigureRefresh(refresh => + { + // Default cache expiration is 30 seconds + refresh.Register("eShopWeb:Settings:NoResultsMessage").SetCacheExpiration(TimeSpan.FromSeconds(10)); + }) + .UseFeatureFlags(featureFlagOptions => + { + // Default cache expiration is 30 seconds + featureFlagOptions.CacheExpirationInterval = TimeSpan.FromSeconds(10); + }); + }); +} + // blazor configuration var configSection = builder.Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME); builder.Services.Configure(configSection); @@ -96,6 +127,12 @@ var app = builder.Build(); +if (useAppConfig) +{ + // Use Azure App Configuration middleware for dynamic configuration refresh. + app.UseAzureAppConfiguration(); +} + app.Logger.LogInformation("App created..."); app.Logger.LogInformation("Seeding Database..."); diff --git a/src/Web/Properties/launchSettings.json b/src/Web/Properties/launchSettings.json index 4dfb1e9d..edbd81b6 100644 --- a/src/Web/Properties/launchSettings.json +++ b/src/Web/Properties/launchSettings.json @@ -22,7 +22,10 @@ "launchBrowser": true, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" + "ASPNETCORE_ENVIRONMENT": "Development", + "AZURE_TENANT_ID": "{azure-tenant-id}", + "AZURE_CLIENT_ID": "{azure-client-id}", + "AZURE_CLIENT_SECRET": "{azure-client-secret}" }, "applicationUrl": "https://localhost:5001;http://localhost:5000" }, diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index 108495e8..68a49f60 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -21,7 +21,9 @@ + + @@ -34,6 +36,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Web/appsettings.json b/src/Web/appsettings.json index 70989a6a..d7d44636 100644 --- a/src/Web/appsettings.json +++ b/src/Web/appsettings.json @@ -16,5 +16,13 @@ "System": "Warning" }, "AllowedHosts": "*" - } + }, + + "eShopWeb": { + "Settings": { + "NoResultsMessage": "THERE ARE NO RESULTS THAT MATCH YOUR SEARCH" + } + }, + "UseAppConfig": false, + "AppConfigEndpoint": "{appconfig-endpoint}" } \ No newline at end of file