Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I inject Options into the View? #916

Closed
guardrex opened this issue Sep 11, 2015 · 3 comments
Closed

How do I inject Options into the View? #916

guardrex opened this issue Sep 11, 2015 · 3 comments

Comments

@guardrex
Copy link
Contributor

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?)

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

Startup

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

public class HomeController : Controller
{
    public HomeController(IOptions<AppOptions> 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"?
@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....

@pranavkm
Copy link
Contributor

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">

@guardrex
Copy link
Contributor Author

@pranavkm Ah! Gotcha. Thanks.

@Spencerooni
Copy link

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

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

@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants