From 500386ea8ac29f38ad82f86bffcdc5908618806a Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Wed, 6 Sep 2017 09:42:26 -0500 Subject: [PATCH] Update UsingOptions sample app (#4214) * Update UsingOptions sample app Takes sample to 2.0 * Update with view option injection Fixes #437 Update Update --- aspnetcore/fundamentals/configuration.md | 8 +- .../Controllers/HomeController.cs | 1 + .../Controllers/HomeController2.cs | 3 +- .../Controllers/HomeController3.cs | 24 ++++++ .../src/UsingOptions/Models/MyOptions.cs | 17 ++-- .../src/UsingOptions/Models/MySubOptions.cs | 19 +++-- .../sample/src/UsingOptions/Program.cs | 20 +++-- .../Properties/launchSettings.json | 19 ----- .../sample/src/UsingOptions/Startup.cs | 10 +-- .../sample/src/UsingOptions/Startup2.cs | 10 +-- .../sample/src/UsingOptions/Startup3.cs | 16 ++-- .../sample/src/UsingOptions/Startup4.cs | 75 +++++++++--------- .../src/UsingOptions/UsingOptions.csproj | 19 +---- .../src/UsingOptions/Views/Home/Index.cshtml | 16 +++- .../sample/src/UsingOptions/appsettings.json | 1 - .../src/UsingOptions/wwwroot/favicon.ico | Bin 0 -> 17174 bytes 16 files changed, 131 insertions(+), 127 deletions(-) create mode 100644 aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController3.cs delete mode 100644 aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Properties/launchSettings.json create mode 100644 aspnetcore/fundamentals/configuration/sample/src/UsingOptions/wwwroot/favicon.ico diff --git a/aspnetcore/fundamentals/configuration.md b/aspnetcore/fundamentals/configuration.md index e178ee7fc655..60cf26470d76 100644 --- a/aspnetcore/fundamentals/configuration.md +++ b/aspnetcore/fundamentals/configuration.md @@ -97,7 +97,7 @@ The options class must be non-abstract with a public parameterless constructor. In the following code, the JSON configuration provider is enabled. The `MyOptions` class is added to the service container and bound to configuration. -[!code-csharp[Main](configuration/sample/src/UsingOptions/Startup.cs?name=snippet1&highlight=8,20-22)] +[!code-csharp[Main](configuration/sample/src/UsingOptions/Startup.cs?name=snippet1&highlight=8,20-21)] The following [controller](../mvc/controllers/index.md) uses [constructor Dependency Injection](xref:fundamentals/dependency-injection#what-is-dependency-injection) on [`IOptions`](https://docs.microsoft.com/aspnet/core/api/microsoft.extensions.options.ioptions-1) to access settings: @@ -135,7 +135,7 @@ Using the following *appsettings.json* file: The `MySubOptions` class: -[!code-csharp[Main](configuration/sample/src/UsingOptions/Models/MySubOptions.cs)] +[!code-csharp[Main](configuration/sample/src/UsingOptions/Models/MySubOptions.cs?name=snippet1)] With the following `Controller`: @@ -143,6 +143,10 @@ With the following `Controller`: `subOption1 = subvalue1_from_json, subOption2 = 200` is returned. +You can also supply options in a view model or inject `IOptions` directly into a view: + +[!code-html[Main](configuration/sample/src/UsingOptions/Views/Home/Index.cshtml?highlight=3-4,16-17,20-21)] + ## IOptionsSnapshot diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController.cs b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController.cs index f7dd48dca187..b73fe15d7491 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController.cs +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController.cs @@ -2,6 +2,7 @@ #if First using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using UsingOptions.Models; namespace UsingOptions.Controllers { diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController2.cs b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController2.cs index 3c77d07ce320..06f6ad067c4c 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController2.cs +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController2.cs @@ -1,8 +1,9 @@ -#define First +//#define First #if First // use with Startup3.cs using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using UsingOptions.Models; namespace UsingOptions.Controllers { diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController3.cs b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController3.cs new file mode 100644 index 000000000000..c3bd8f6d3c16 --- /dev/null +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Controllers/HomeController3.cs @@ -0,0 +1,24 @@ +//#define First +#if First +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using UsingOptions.Models; + +namespace UsingOptions.Controllers +{ + public class HomeController : Controller + { + private readonly MyOptions _options; + + public HomeController(IOptions optionsAccessor) + { + _options = optionsAccessor.Value; + } + + public IActionResult Index() + { + return View(_options); + } + } +} +#endif diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Models/MyOptions.cs b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Models/MyOptions.cs index 2bb562cc8785..e503369728e4 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Models/MyOptions.cs +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Models/MyOptions.cs @@ -1,10 +1,13 @@ -public class MyOptions +namespace UsingOptions.Models { - public MyOptions() + public class MyOptions { - // Set default value. - Option1 = "value1_from_ctor"; + public MyOptions() + { + // Set default value. + Option1 = "value1_from_ctor"; + } + public string Option1 { get; set; } + public int Option2 { get; set; } = 5; } - public string Option1 { get; set; } - public int Option2 { get; set; } = 5; -} \ No newline at end of file +} diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Models/MySubOptions.cs b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Models/MySubOptions.cs index 8aa1ebb2f268..48b3e4e22952 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Models/MySubOptions.cs +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Models/MySubOptions.cs @@ -1,11 +1,16 @@ -public class MySubOptions +namespace UsingOptions.Models { - public MySubOptions() + #region snippet1 + public class MySubOptions { - // Set default values. - SubOption1 = "value1_from_ctor"; - SubOption2 = 5; + public MySubOptions() + { + // Set default values. + SubOption1 = "value1_from_ctor"; + SubOption2 = 5; + } + public string SubOption1 { get; set; } + public int SubOption2 { get; set; } } - public string SubOption1 { get; set; } - public int SubOption2 { get; set; } + #endregion } diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Program.cs b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Program.cs index 82d3f0f532cf..713c9964bb88 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Program.cs +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Program.cs @@ -1,21 +1,19 @@ -using System.IO; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; namespace UsingOptions { - public static class Program + public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseStartup() .Build(); - - host.Run(); - } } -} \ No newline at end of file +} diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Properties/launchSettings.json b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Properties/launchSettings.json deleted file mode 100644 index fcf009586619..000000000000 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Properties/launchSettings.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:30683/", - "sslPort": 0 - } - }, - "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} \ No newline at end of file diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup.cs b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup.cs index 01d5169ac896..4f160b10f4a5 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup.cs +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using UsingOptions.Models; namespace UsingOptions { @@ -35,15 +36,12 @@ public void ConfigureServices(IServiceCollection services) services.AddMvc(); } #endregion - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, - ILoggerFactory loggerFactory) + // This method is called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app) { - loggerFactory.AddConsole(); - app.UseDeveloperExceptionPage(); app.UseMvcWithDefaultRoute(); } } } -#endif \ No newline at end of file +#endif diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup2.cs b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup2.cs index 53b8174c394e..30614736afc6 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup2.cs +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup2.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using UsingOptions.Models; namespace UsingOptions { @@ -22,7 +23,7 @@ public Startup(IHostingEnvironment env) public IConfigurationRoot Configuration { get; set; } - // This method gets called by the runtime. Use this method to add services to the container. + // This method is called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 #region snippet1 public void ConfigureServices(IServiceCollection services) @@ -43,12 +44,9 @@ public void ConfigureServices(IServiceCollection services) services.AddMvc(); } #endregion - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, - ILoggerFactory loggerFactory) + // This method is called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app) { - loggerFactory.AddConsole(); - app.UseDeveloperExceptionPage(); app.UseMvcWithDefaultRoute(); } diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup3.cs b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup3.cs index ef4c3935ac55..fc49e0bcfc75 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup3.cs +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup3.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using UsingOptions.Models; namespace UsingOptions { @@ -23,9 +24,9 @@ public Startup(IHostingEnvironment env) public IConfigurationRoot Configuration { get; set; } - // This method gets called by the runtime. Use this method to add services to the container. + // This method is called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 -#region snippet1 + #region snippet1 public void ConfigureServices(IServiceCollection services) { // Adds services required for using options. @@ -47,16 +48,13 @@ public void ConfigureServices(IServiceCollection services) // Add framework services. services.AddMvc(); } -#endregion - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, - ILoggerFactory loggerFactory) + #endregion + // This method is called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app) { - loggerFactory.AddConsole(); - app.UseDeveloperExceptionPage(); app.UseMvcWithDefaultRoute(); } } } -#endif \ No newline at end of file +#endif diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup4.cs b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup4.cs index 000105554867..11d0b43dad5a 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup4.cs +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Startup4.cs @@ -5,53 +5,50 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -public class Startup +using UsingOptions.Models; + +namespace UsingOptions { - #region snippet1 - public Startup(IHostingEnvironment env) + public class Startup { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - Configuration = builder.Build(); - } - #endregion + #region snippet1 + public Startup(IHostingEnvironment env) + { + var builder = new ConfigurationBuilder() + .SetBasePath(env.ContentRootPath) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) + .AddEnvironmentVariables(); - public IConfigurationRoot Configuration { get; } + Configuration = builder.Build(); + } + #endregion - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - // Add framework services. - services.AddMvc(); - } + public IConfigurationRoot Configuration { get; } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) - { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - app.UseBrowserLink(); - } - else + // This method is called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) { - app.UseExceptionHandler("/Home/Error"); + // Add framework services. + services.AddMvc(); } - app.UseStaticFiles(); - - app.UseMvc(routes => + // This method is called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { - routes.MapRoute( - name: "default", - template: "{controller=Home}/{action=Index}/{id?}"); - }); + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + app.UseBrowserLink(); + } + else + { + app.UseExceptionHandler("/Home/Error"); + } + + app.UseStaticFiles(); + app.UseMvcWithDefaultRoute(); + } } } -#endif \ No newline at end of file +#endif diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/UsingOptions.csproj b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/UsingOptions.csproj index c1cd2acba46e..d7c2e491fc27 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/UsingOptions.csproj +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/UsingOptions.csproj @@ -1,26 +1,11 @@ - netcoreapp1.0 - true - UsingOptions - Exe - UsingOptions - 1.0.4 - $(PackageTargetFallback);dotnet5.6;portable-net45+win8 + netcoreapp2.0 - - - - - - - - - - + diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Views/Home/Index.cshtml b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Views/Home/Index.cshtml index 325f6c32b235..a4273cbe3e86 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Views/Home/Index.cshtml +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/Views/Home/Index.cshtml @@ -1,11 +1,23 @@ -@using UsingOptions.Models +@using Microsoft.Extensions.Options +@using UsingOptions.Models @model MyOptions +@inject IOptions OptionsAccessor - + + + + Using Options sample app +

Options

+ +

Options provided by the model

Option1: @Model.Option1

Option2: @Model.Option2

+ +

Options injected into the view

+

Option1: @OptionsAccessor.Value.Option1

+

Option2: @OptionsAccessor.Value.Option2

diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/appsettings.json b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/appsettings.json index fe8efbcf127e..ad45aa9a73f3 100644 --- a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/appsettings.json +++ b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/appsettings.json @@ -1,7 +1,6 @@ { "option1": "value1_from_json", "option2": -1, - "subsection": { "suboption1": "subvalue1_from_json", "suboption2": 200 diff --git a/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/wwwroot/favicon.ico b/aspnetcore/fundamentals/configuration/sample/src/UsingOptions/wwwroot/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..bfe873eb228f98720fe0ed18c638daa13906958f GIT binary patch literal 17174 zcmeHOJ#Q015PgP|sUltJ%A8D-(o!TUkc{NQ1+uPjPZg!4M<`cP($XUeDk>yUiu?k8 z10_|E2qK~~GdH`@9=;55lyHgjMj7wN?OyiY%vw?RV_+}{(X$}` zkiRp}d#2#VM9RE@(KQaI zUnaWs7TXteX8N); ON)^VQz<8Ga>-ryC<7BY_ literal 0 HcmV?d00001