Skip to content

Commit

Permalink
Update UsingOptions sample app (dotnet#4214)
Browse files Browse the repository at this point in the history
* Update UsingOptions sample app

Takes sample to 2.0

* Update with view option injection

Fixes dotnet#437

Update

Update
  • Loading branch information
guardrex authored and scottaddie committed Sep 6, 2017
1 parent 725d2b5 commit 500386e
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 127 deletions.
8 changes: 6 additions & 2 deletions aspnetcore/fundamentals/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TOptions>`](https://docs.microsoft.com/aspnet/core/api/microsoft.extensions.options.ioptions-1) to access settings:

Expand Down Expand Up @@ -135,14 +135,18 @@ 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`:

[!code-csharp[Main](configuration/sample/src/UsingOptions/Controllers/HomeController2.cs?name=snippet1)]

`subOption1 = subvalue1_from_json, subOption2 = 200` is returned.

You can also supply options in a view model or inject `IOptions<TOptions>` directly into a view:

[!code-html[Main](configuration/sample/src/UsingOptions/Views/Home/Index.cshtml?highlight=3-4,16-17,20-21)]

<a name=in-memory-provider></a>

## IOptionsSnapshot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#if First
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using UsingOptions.Models;

namespace UsingOptions.Controllers
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MyOptions> optionsAccessor)
{
_options = optionsAccessor.Value;
}

public IActionResult Index()
{
return View(_options);
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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<Startup>()
.Build();

host.Run();
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using UsingOptions.Models;

namespace UsingOptions
{
Expand Down Expand Up @@ -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
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using UsingOptions.Models;

namespace UsingOptions
{
Expand All @@ -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)
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using UsingOptions.Models;

namespace UsingOptions
{
Expand All @@ -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.
Expand All @@ -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
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -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
#endif
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>UsingOptions</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>UsingOptions</PackageId>
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit 500386e

Please sign in to comment.