доступ к Web.config файлу

 
 

из разметки

Скрыть

Показать

Копировать
  Web.config  
<?xml version="1.0"?>
<configuration>
 <system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />
 </system.web>
 <!--секция для пользовательских настроек-->
 <appSettings>
  <add key="enMessage" value="Hello World!"/>
  <add key="ruMessage" value="Привет Мир!"/>
 </appSettings>
</configuration>
Скрыть

Показать

Копировать
  Default.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <%-- $ имя секции:имя ключа --%>
   <asp:Label ID="Label1" runat="server" Text="<%$ AppSettings:enMessage %>"></asp:Label>
   <br />
   <%-- $ имя секции:имя ключа --%>
   <asp:Label ID="Label2" runat="server" Text="<%$ AppSettings:ruMessage %>"></asp:Label>
  </div>
 </form>
</body>
</html>
 
 

из кода

Скрыть

Показать

Копировать
  Web.config  
<?xml version="1.0"?>
<configuration>
 <system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />
 </system.web>
 <!--секция для пользовательских настроек-->
 <appSettings>
  <add key="enMessage" value="Hello World!"/>
  <add key="ruMessage" value="Привет Мир!"/>
 </appSettings>
</configuration>
Скрыть

Показать

Копировать
  Default.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
   <br />
   <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
  </div>
 </form>
</body>
</html>
Скрыть

Показать

Копировать
  Default.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
//добавить
using System.Configuration;
 
public partial class _Default : System.Web.UI.Page {
 protected void Page_Load(object sender, EventArgs e) {
  Label1.Text = ConfigurationManager.AppSettings["enMessage"];
  Label2.Text = ConfigurationManager.AppSettings["ruMessage"];
 }
}