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,為避免將資料寫死於程式裡形成 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>
由於 appSettings 屬性是 key-value 型別,設置方法很簡單,將自行定義的名稱賦值給 key,這裡可看作為參數的名稱,而該參數欲存放的資料值給 value
<appSettings>
<add key="Greeting" value="Hello World" />
</appSettings>
接著從 Controller 中讀取 appSettings,首先引用 System.Configuration
Namespace,讀取方式也非常簡單
using System.Configuration;
public ActionResult GetAppSettingsProperty()
{
var message = ConfigurationManager.AppSettings["Greeting"].ToString();
return Content(message);
}
打完,收工!
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
The Future of ASP.NET: What to Expect
read article
DevExpress - Enhancing ASP.NET Web Forms with the ASPxGridView Control
read article
Advanced Search in .NET with Elasticsearch(Full Video)
read article
Introducing Brick SaaS Starter Kit - Launch SaaS Products Faster
read article
Using server sent events in ASP.NET
read article
Important Links
read article
Serverless OAuth2/OIDC server with OpenIddict 6 and RDS Aurora v2
read article
Learning in Reverse: How I Would Learn ASP. Net Core and Entity Framework If I Could Go Back In Time
read article
Dependency injection validation error in ASP.NET Core projects
read article
Agrupamiento de datos de una lista usando LINQ en C#
read article
Asp .Net: Create a Simple 'Web User Control'
read article
[ASP.NET] 如何導向到錯誤頁面
read article
DevExpress - Simplifying Server-to-Client Data Transfer with ASPxCallback JSProperties
read article
Asp.net
read article
[ASP.NET] 設置與取得 Web.config 自定義資料
currently reading
How to Hire Dedicated .NET Developers
read article
Permission-Based Authorization in ASP.NET Core: A Step-by-Step Guide
read article
Permission-Based Authorization in ASP.NET Core: A Step-by-Step Guide
read article
Dependency Container and Services Lifetimes (На русском)
read article
Containerize ASP.NET Core API, Entity Framework with SQL Server, Let's Encrypt, Docker, and Nginx (Part 1)
read article
differences of Transient and scoped in ASP NET
read article
ASP.NET8 using DataTables.net – Part6 – Returning additional parameters in AJAX
read article
ASP.NET8 using DataTables.net – Part4 – Multilingual
read article
ASP.NET8 using DataTables.net – Part8 – Select rows
read article
ASP.NET8 using DataTables.net – Part3 – State saving
read article
ASP.NET8 using DataTables.net – Part7 – Buttons regular
read article
ASP.NET8 using DataTables.net – Part5 – Passing additional parameters in AJAX
read article
ASP.NET8 using DataTables.net – Part9 – Advanced Filters
read article
ASP.NET8 using DataTables.net – Part2 – Action buttons
read article
ASP.NET 8 – Multilingual Application with single Resx file – Part 3 – Forms Validation Strings
read article
Featured ones: