0

I need to access the value that is inside web.config appSettings from my cshtml file

here is my code inside the cshtml file:

<body>
    <div>
        @RenderBody()
        <footer>
            <p  @System.Configuration.ConfigurationManager.AppSettings["myKey"]</p>
        </footer>
    </div>

...
</body>

And this is my code from web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
   <add key="myKey" value="MyValue"/>
</appSettings>
</configuration>

Thanks in advance

Yiyi You
  • 16,875
  • 1
  • 10
  • 22
  • So what's the issue with this code. The only mistake I can see here is that you haven't close the starting

    tag properly.

    – Arib Yousuf Mar 02 '21 at 09:21
  • I actually can access the value from app.config but not from web.config. Do you know what could cause the problem to access it from web.config? – albus dumbledore Mar 02 '21 at 09:25
  • Are you using .NET core? – Arib Yousuf Mar 02 '21 at 10:09
  • Yes exactly, I use .Net core – albus dumbledore Mar 02 '21 at 11:05
  • In .NET core you can't read from web.config. For references: https://stackoverflow.com/questions/40186445/access-web-config-settings-in-asp-net-core-app https://stackoverflow.com/questions/46996480/how-to-read-web-config-file-in-net-core-app https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?tabs=basicconfiguration&view=aspnetcore-5.0 – Arib Yousuf Mar 02 '21 at 12:05

1 Answers1

0

Firstly,System.Configuration.ConfigurationManager is used in .net framework rather than .net core.In.net core,we usually configure in appsettings.json.You can refer to the official link.Here is a demo:

Startup.cs:

public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
           
            ...
            services.AddSingleton<IConfiguration>(Configuration);
        }

appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "data source=exmaple;initial catalog=example;persist security info=True;user id=example;password=example"
  },
  "myKey": "myValue"
}

TestController:

public class TestController : Controller
    {
        IConfiguration _iconfiguration;
        public TestController(IConfiguration iconfiguration)
        {
            _iconfiguration = iconfiguration;
        }
        public IActionResult TestData() {
            ViewBag.Data = _iconfiguration.GetSection("myKey").Value;
            return View();
        }
    }

TestData.cshtml:

<p> @ViewBag.data</p>

result: enter image description here

Yiyi You
  • 16,875
  • 1
  • 10
  • 22