Logo

dev-resources.site

for different kinds of informations.

[ASP.NET] 設置與取得 Web.config 自定義資料

Published at
11/25/2024
Categories
aspnet
Author
fakestandard
Categories
1 categories in total
aspnet
open
Author
12 person written this
fakestandard
open
[ASP.NET] 設置與取得 Web.config 自定義資料

在 ASP.NET 應用程式裡,我們可將動態資料儲存於組態檔之中,也就是 Web.config,為避免將資料寫死於程式裡形成 hardcode,未來程式上線後,可讓維護者僅透過修改組態檔資料,即可改變程式的參數賦值,而無須重新修改程式碼

在 Web.config 找到 appSettings 屬性,以下為預設的設置。

  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
Enter fullscreen mode Exit fullscreen mode

由於 appSettings 屬性是 key-value 型別,設置方法很簡單,將自行定義的名稱賦值給 key,這裡可看作為參數的名稱,而該參數欲存放的資料值給 value

<appSettings>
    <add key="Greeting" value="Hello World" />
</appSettings>
Enter fullscreen mode Exit fullscreen mode

接著從 Controller 中讀取 appSettings,首先引用 System.Configuration Namespace,讀取方式也非常簡單

using System.Configuration;

public ActionResult GetAppSettingsProperty()
{
     var message = ConfigurationManager.AppSettings["Greeting"].ToString();

     return Content(message);
}
Enter fullscreen mode Exit fullscreen mode

打完,收工!


Thanks for reading the article 🌷 🌻 🌼

If you like it, please don't hesitate to click heart button ❤️
or follow my GitHub ⭐ I'd appreciate it.


aspnet Article's
30 articles in total
Favicon
The Future of ASP.NET: What to Expect
Favicon
DevExpress - Enhancing ASP.NET Web Forms with the ASPxGridView Control
Favicon
Advanced Search in .NET with Elasticsearch(Full Video)
Favicon
Introducing Brick SaaS Starter Kit - Launch SaaS Products Faster
Favicon
Using server sent events in ASP.NET
Favicon
Important Links
Favicon
Serverless OAuth2/OIDC server with OpenIddict 6 and RDS Aurora v2
Favicon
Learning in Reverse: How I Would Learn ASP. Net Core and Entity Framework If I Could Go Back In Time
Favicon
Dependency injection validation error in ASP.NET Core projects
Favicon
Agrupamiento de datos de una lista usando LINQ en C#
Favicon
Asp .Net: Create a Simple 'Web User Control'
Favicon
[ASP.NET] 如何導向到錯誤頁面
Favicon
DevExpress - Simplifying Server-to-Client Data Transfer with ASPxCallback JSProperties
Favicon
Asp.net
Favicon
[ASP.NET] 設置與取得 Web.config 自定義資料
Favicon
How to Hire Dedicated .NET Developers
Favicon
Permission-Based Authorization in ASP.NET Core: A Step-by-Step Guide
Favicon
Permission-Based Authorization in ASP.NET Core: A Step-by-Step Guide
Favicon
Dependency Container and Services Lifetimes (На русском)
Favicon
Containerize ASP.NET Core API, Entity Framework with SQL Server, Let's Encrypt, Docker, and Nginx (Part 1)
Favicon
differences of Transient and scoped in ASP NET
Favicon
ASP.NET8 using DataTables.net – Part6 – Returning additional parameters in AJAX
Favicon
ASP.NET8 using DataTables.net – Part4 – Multilingual
Favicon
ASP.NET8 using DataTables.net – Part8 – Select rows
Favicon
ASP.NET8 using DataTables.net – Part3 – State saving
Favicon
ASP.NET8 using DataTables.net – Part7 – Buttons regular
Favicon
ASP.NET8 using DataTables.net – Part5 – Passing additional parameters in AJAX
Favicon
ASP.NET8 using DataTables.net – Part9 – Advanced Filters
Favicon
ASP.NET8 using DataTables.net – Part2 – Action buttons
Favicon
ASP.NET 8 – Multilingual Application with single Resx file – Part 3 – Forms Validation Strings

Featured ones: