I make a small web application for working with nail polishes.

And I need to somehow visually determine whether I am currently working on the production version of the site or on the developer, I usually look at the appearance of the logo in the header: on the production it looks like this

prod logo

And the dev version looks like this:

dev logo

And now it is done through the analysis of the address bar, in the Views / Shared / _Layout.cshtml file:

@functions{ private bool IsDevEnv() { return Context.Request.Host.Value.Contains("localhost"); } } <!DOCTYPE html> <html> .... @if (IsDevEnv()) { <a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">Polish <span>Hub</span></a> } else { <a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">Polish Hub</a> } 

However, this is not exactly what I need: I need to check the parameters of the sql server connection string, not the hostname of the server. (Unfortunately, these are the features of the appharbor for working with the core for the current day - in the classic asp.net mvc, variable changes normally occur)

How can I get to these settings (connection string) in the asp.net core?

Is it possible to somehow get to the Configuration.GetConnectionString in view? Or, if it is not possible, then from which level to define the mode variable (prod / dev) in order to forward it to the page layout.

  • When I saw a quick glance at your version of the dev версия , I read it as PornHub, I'm afraid I’m not the only one who will have such associations, something needs to be done with this :) - Bulson
  • @Bulson Well, well done, you saw it for the first time ... FROM APRIL FIRST !!! - AK
  • I just said that no one has played me up today, and on you :) - Bulson

2 answers 2

The settings in the View can be reached via @inject

Suppose we have written in appSettings.json

 { "SomeOption" : { "SomeProp" : 1000 } } 

Create a class MyAppOptions and in it create a property with the same name as the option

 public int SomeProp { get; set; } 

Now let's go to Startup.cs in it, we should already have a field

 private readonly IConfiguration _configuration; 

which is assigned a value through the constructor. Next, go to the ConfigureServices(...) method and add this

 services.Configure<MyAppOptions>(_configuration.GetSection("SomeOption")); 

Now in View

 @using Microsoft.Extensions.Options @inject IOptions<MyAppOptions> options @в нужном месте@ @options.Value.SomeProp 

    Thank you @Bullson for indicating the correct answer, did that for yourself:

    In startup.cs:

     services.Configure<MyAppOptions>(options => { options.SqlConnectionStringBuilder = new SqlConnectionStringBuilder(Configuration["ConnectionStrings:DefaultConnection"]); }); 

    The class of settings itself:

     public class MyAppOptions { public SqlConnectionStringBuilder SqlConnectionStringBuilder { get; set; } public bool IsDevEnv => this.SqlConnectionStringBuilder.IntegratedSecurity; } 

    (I used the fact that I enter the dev environment without entering the windows login password)

    And in Views / Shared / _Layout.cshtml I do inject settings:

     @using AuroraBorealis.PolishHub.WebApp.Data @using Microsoft.Extensions.Options @inject IOptions<MyAppOptions> options <!DOCTYPE html> <html> .... @if (options.Value.IsDevEnv) { <a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">Polish <span>Hub</span></a> } else { <a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">Polish Hub</a> } 

    Related Links:

    • one
      But in general, the condition takes into account the date: the first of April IsDevEnv is inverted: polishhub.apphb.com ;) - AK