Aspnetcore: ビューにオプションを挿入するにはどうすればよいですか?

作成日 2015年09月11日  ·  3コメント  ·  ソース: dotnet/aspnetcore

オプションで保持し、使用されるビューに挿入しようとしている構成からのAzureCDNエンドポイントがあります。 私は立ち往生しています.... NETドキュメントにはビューオプションインジェクションのマークアップコードが表示されておらず、 Rickの投稿はbeta7用に更新されていないようです。 私はdnx-coreclr-win-x64.1.0.0-beta7を使用しており、これはdnxcore50アプリです。

モデル

(ここでのベストプラクティスについて混乱しています:これはモデルにも当てはまりますか?)

`` `c#
名前空間MyApp.Models
{{
パブリッククラスAppOptions
{{
パブリック文字列CDN {get; セットする; }
}
}

#### 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#
パブリッククラスHomeController:コントローラー
{{
public HomeController(IOptionsoptionsAccessor)
{{
オプション= 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>
...

... <strong i="37">@model</strong> MyApp.Models.AppOptions@{Model.CDN}.vo...または@Model.CDN.vo...を使用した場合も同じ結果になります。

最も参考になるコメント

他の人にとっては、注入するインターフェースを完全に修飾する必要があることを忘れないでください。

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

全てのコメント3件

IOptionsはDIコンテナにあるものなので、 <strong i="6">@inject</strong> IOptions<AppOptions> AppOptionsAccessorにする必要があります

かみそりの波線の場合、間違った括弧を使用しているという問題があります。

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

@pranavkmああ! ガッチャ。 ありがとう。

他の人にとっては、注入するインターフェースを完全に修飾する必要があることを忘れないでください。

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

このページは役に立ちましたか?
0 / 5 - 0 評価