Aspnetcore: Wie füge ich Optionen in die Ansicht ein?

Erstellt am 11. Sept. 2015  ·  3Kommentare  ·  Quelle: dotnet/aspnetcore

Ich habe einen Azure CDN-Endpunkt aus der Konfiguration, den ich in Optionen halten und in Ansichten einfügen möchte, wo er verwendet wird. Ich stecke fest ... die .NET -Dokumentation zeigt den Markup-Code für die Injektion von Ansichtsoptionen nicht und Ricks Post scheint nicht für Beta7 aktualisiert zu sein. Ich bin auf dnx-coreclr-win-x64.1.0.0-beta7 und dies ist eine dnxcore50-App.

Modell

(Verwirrt bei Best Practices hier: Geht das überhaupt in Models?)

``` c#
Namespace MyApp.Models
{
AppOptions der öffentlichen Klasse
{
öffentliche Zeichenfolge CDN { erhalten; einstellen; }
}
}

#### 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#
öffentliche Klasse HomeController : Controller
{
öffentlicher HomeController (IOptionsOptionenAccessor)
{
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 `<strong i="32">@model</strong> MyApp.Models.AppOptions`
- with inject `<strong i="33">@inject</strong> 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
<strong i="34">@inject</strong> 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>
...

... gleiches Ergebnis, wenn ich <strong i="37">@model</strong> MyApp.Models.AppOptions und @{Model.CDN}.vo... oder @Model.CDN.vo... verwende.

Hilfreichster Kommentar

Denken Sie für alle anderen daran, dass Sie die Schnittstelle zum Injizieren vollständig qualifizieren müssen.

<strong i="6">@inject</strong> Microsoft.Extensions.Options.IOptions<AppOptions> AppOptionsAccessor

Alle 3 Kommentare

Da IOptions derjenige ist, der sich im DI-Container befindet, müssten Sie <strong i="6">@inject</strong> IOptions<AppOptions> AppOptionsAccessor

Bei den Razor-Schnörkeln besteht das Problem darin, dass Sie die falschen Klammern verwenden:

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

@pranavkm Ah! Erwischt. Danke.

Denken Sie für alle anderen daran, dass Sie die Schnittstelle zum Injizieren vollständig qualifizieren müssen.

<strong i="6">@inject</strong> Microsoft.Extensions.Options.IOptions<AppOptions> AppOptionsAccessor

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen