spottedmahn
spottedmahn

Reputation: 16011

Access Configuration from a View in ASP.NET Core

How can I access IConfiguration in an MVC View in ASP.NET Core 1.1?

Ex. _Layout.cshtml

<link href="@Configuration["MyConfigEntry"]/css/site.min.css>

Upvotes: 33

Views: 12521

Answers (3)

David Jones
David Jones

Reputation: 3382

@inject it into the view (if that's really what you want to do).

Additional documentation: Options provided by a view model or with direct view injection

Upvotes: 11

Shyju
Shyju

Reputation: 218832

In asp.net core , you can inject IConfiguration implementation to your views and use that :)

@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<link rel="stylesheet" href="@Configuration["MyConfigEntry"]/css/site.min.css">

Upvotes: 71

Orel Eraki
Orel Eraki

Reputation: 12196

If the posted link is where you took your example, then Probably by doing

<link href="@Startup.Configuration["MyConfigEntry"]/css/site.min.css>

This is because you set the Configuration object as static while bootstrap the application.

From Configure an ASP.NET Core App

public static IConfigurationRoot Configuration { get; set; }

Upvotes: 3

Related Questions