Aspnetcore: How do I inject Options into the View?

Created on 11 Sep 2015  路  3Comments  路  Source: dotnet/aspnetcore

I have an Azure CDN endpoint coming from Configuration that I'm trying to hold in Options and inject into views where it will be used. I'm stuck ... the .NET docs don't show the markup code for view options injection and Rick's post doesn't seem to be updated for beta7. I'm on dnx-coreclr-win-x64.1.0.0-beta7, and this is a dnxcore50 app.

Model

(Confused on best practices here: Does this even go in Models?)

``` c#
namespace MyApp.Models
{
public class AppOptions
{
public string CDN { get; set; }
}
}

#### Startup

``` c#
public class Startup
{
    public IConfiguration Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        var configurationBuilder = new ConfigurationBuilder().AddEnvironmentVariables();
        Configuration = configurationBuilder.Build();
        Configuration["CDN"] = "az123456";
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOptions();
        services.Configure<AppOptions>(Configuration);
        services.AddSingleton(_ => Configuration);
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseErrorPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseErrorHandler("/error");
        }
        app.UseStaticFiles();
        app.UseMvcWithDefaultRoute();
    }
}

HomeController

``` c#
public class HomeController : Controller
{
public HomeController(IOptions optionsAccessor)
{
Options = optionsAccessor.Options;
}

AppOptions Options { get; }

[Route("/error")]
public IActionResult Script() => File("/wwwroot/error.htm", "text/html");

[HttpGet]
public IActionResult Index()
{
    return View("index", Options);
}

}

#### Markup

If everything else above is ok, the markup part is unclear to me. How do I inject this?
- as a model with `@model MyApp.Models.AppOptions`
- with inject `@inject MyApp.Models.AppOptions AppOptions`
- is the problem with the reference in the `src=` ... how do I break the property lookup at the "CDN" before the period prior to "vo"?

``` html
@inject MyApp.Models.AppOptions AppOptions
...
<body>
    <!-- Error (squiggles) on "AppOptions"  and complains it can't be found -->
    <img src="http://@{AppOptions.CDN}.vo.msecnd.net/container/image.png" alt="Image from CDN">
    <!-- Error (squiggles) on "vo" and complains its trying to lookup "vo" on the property -->
    <img src="http://@AppOptions.CDN.vo.msecnd.net/container/image.png" alt="Image from CDN">
</body>
...

... same result if I use @model MyApp.Models.AppOptions and @{Model.CDN}.vo... or @Model.CDN.vo....

Most helpful comment

For anyone else, remember you have to fully qualify the interface to inject.

@inject Microsoft.Extensions.Options.IOptions<AppOptions> AppOptionsAccessor

All 3 comments

Since IOptions is the one that in the DI container, you'd need to @inject IOptions<AppOptions> AppOptionsAccessor

For the Razor squiggles, is the issue that you're using the wrong parantheses:

<img src="http://@(AppOptionsAccessor.Options.CDN).vo.msecnd.net/container/image.png" alt="Image from CDN">

@pranavkm Ah! Gotcha. Thanks.

For anyone else, remember you have to fully qualify the interface to inject.

@inject Microsoft.Extensions.Options.IOptions<AppOptions> AppOptionsAccessor

Was this page helpful?
0 / 5 - 0 ratings

Related issues

snebjorn picture snebjorn  路  3Comments

UweKeim picture UweKeim  路  3Comments

markrendle picture markrendle  路  3Comments

Kevenvz picture Kevenvz  路  3Comments

dotNETSanta picture dotNETSanta  路  3Comments