5

I was following the following official documentation: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#access-configuration-in-razor-pages

This implementation of @Configuration["myKey"] works perfectly for code that is not in between @{} brackets, but when I have a code block it simply does not work.

The documentation provides no code examples as far as I can see...

How do I solve this problem?

The code is of course in a Razor page (.cshtml).

I tried removing the @ and putting in the same code without the @, but then it gives a context error...

P.S. the code in question is a POCO if it matters.

P.P.S. I use @inject IConfiguration Configuration for importing the configuration at the top of the Razor page.

My problematic code:

var website = new WebSite()
        {
    private string altName = Configuration["FrameworkData:PageTitle"] +" - " +Configuration["FrameworkData:Slogan"];
    AlternateName = altName,
....

I've already tried specifying the IConfiguration Type before the Configuration specification without any avail.

UPDATE

My starting code with the problematic parts in it:

@using Microsoft.AspNetCore.Http;
@using Schema.NET;
@model projectname2.Areas.MyFeature.Pages.Shared._LayoutModel
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@{  
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name='description' content='@ViewData["Description"]'>
    <title>@ViewData["Title"] - @Configuration["FrameworkData:PageTitle"]</title>
    @{
        var website = new WebSite()
        {
            private string altName = "";
    string altName = $"{Configuration["FrameworkData:PageTitle"]} - {Configuration["FrameworkData:Slogan"]}";
    AlternateName = altName,
    Name = Configuration["FrameworkData:PageTitle"],
    Url = new Uri("https://example.com")
};
        var jsonLd = website.ToString();
        var address = new PostalAddress()
        {
        StreetAddress = "Example street 10",
        AddressLocality = "NY",
        PostalCode = "11111",
        AddressCountry = "US"
        };
        var geo = new GeoCoordinates()
        {
            Latitude = 0.3947623,
            Longitude = 0.8723408
        };
        var localbusiness = new LocalBusiness()
            {
                Name = "Project name",
                Image = new Uri("https://example.com/graphics/logo.svg"),
                Url = new Uri("https://example.com"),
                Telephone = "1234 1243567",
                Address = address,
                Geo = geo,
                SameAs = new Uri("https://example.com"),
                OpeningHours = "Mo-Fr 08:00-17:00",
            };
        
        var jsonLdlb = localbusiness.ToString();
        var organization = new Organization()
            {
                AreaServed = "US",
                ContactPoint = new ContactPoint()
                {
                    AvailableLanguage = "English",
                    ContactOption = ContactPointOption.TollFree,
                    ContactType = "customer service",
                    Telephone = "1234 1243567"
                },
                Url = new Uri("https://example.com"),
                Logo = new Uri("https://example.com/graphics/logo.svg"),
            };
            var jsonLdorg = organization.ToString();
    }
<script type="application/ld+json">
    @Html.Raw(jsonLd)
</script>
<script type="application/ld+json">
    @Html.Raw(jsonLdlb)
</script>
<script type="application/ld+json">
    @Html.Raw(jsonLdorg)
</script>

2 Answers 2

3
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@{
   string myValue = Configuration["FrameworkData:PageTitle"];
   // Do your things
}

Refer Microsoft Docs

9
  • This is the same just without private before the string?.. This apparently doesn't help me, still getting the same error...
    – Munchkin
    Commented Dec 10, 2021 at 11:09
  • Could you please the full code for the UI? I just want to see how and where you inject things Commented Dec 10, 2021 at 11:20
  • There is no problem with the UI per se. But I'll post the starting code in the updated question, gimme a second.
    – Munchkin
    Commented Dec 10, 2021 at 11:22
  • What happens when you debug this line : Configuration["FrameworkData:PageTitle"]? Did you verify your json file has the correct entry as "FrameworkData:PageTitle" too? Commented Dec 10, 2021 at 11:36
  • What do you mean by debug this line? It's RCL, so of course it itself doesn't contain the JSON file, but it's supposed to be this way. Or is it out of scope, because the library can't access the file in the main web app?
    – Munchkin
    Commented Dec 10, 2021 at 11:37
0

I had to define the variables outside of the problematic code block in an another code block like this:

@{
    var pageTitle = Configuration["FrameworkData:PageTitle"];
}

I had to define the altName before initializing the WebSite() instance:

var altName = $"{pageTitle} - {slogan}";
var website = new WebSite()

And then I can just reference the variable by variable name.

Case closed.

3
  • that is the same thing I told you before. My comment yesterday "I mean, you can place a breakpoint at " string altName........." and check what values are getting populated for configurations. Or else why you don't want to access those configuration values before creating a new Website??? I mean before this line --> var website = new WebSite() {}" Commented Dec 11, 2021 at 12:04
  • Glad it worked :) Commented Dec 11, 2021 at 12:04
  • Accepted your answer
    – Munchkin
    Commented Dec 11, 2021 at 12:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.