You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6项目中如何在HTML文件中读取AppSettings参数

在.NET 6项目的HTML(Razor视图)中读取配置的可行方法

你之前尝试的<%$ AppSettings: ... %>和ConfigurationManager.AppSettings都是传统ASP.NET Web Forms/Framework的语法,ASP.NET Core(.NET 6)不再支持,以下是适配.NET 6的几种实现方案:


方案1:直接在Razor视图中注入IConfiguration(快速实现)

步骤:

  1. 在.cshtml视图文件顶部注入配置服务:
@inject IConfiguration Configuration
  1. 读取配置项:
    • 读取平级的AddressLine3:
      <p>@Configuration["AddressLine3"]</p>
      
    • 读取嵌套的Address.AddressLine1(用冒号分隔层级):
      <p>@Configuration["Address:AddressLine1"]</p>
      

方案2:强类型配置绑定(推荐,更规范易维护)

步骤:

  1. 创建配置模型类:
// 项目中新建类,比如Models/AddressSettings.cs
public class AddressSettings
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    // 可将AddressLine3加入模型统一管理
    public string AddressLine3 { get; set; }
}
  1. 在Program.cs中注册配置绑定:
// 绑定Address节点到AddressSettings
builder.Services.Configure<AddressSettings>(builder.Configuration.GetSection("Address"));
// 若AddressLine3单独在根节点,可将其加入模型后一起绑定,或单独读取
  1. 在视图中注入IOptions<AddressSettings>:
@inject IOptions<AddressSettings> AddressOptions
  1. 使用配置:
<p>@AddressOptions.Value.AddressLine1</p>
<p>@AddressOptions.Value.AddressLine2</p>
<p>@AddressOptions.Value.AddressLine3</p>

方案3:通过Controller传递配置到视图(MVC场景)

步骤:

  1. 在Controller中注入配置服务:
public class HomeController : Controller
{
    private readonly IConfiguration _config;
    private readonly AddressSettings _addressSettings;

    public HomeController(IConfiguration config, IOptions<AddressSettings> addressOptions)
    {
        _config = config;
        _addressSettings = addressOptions.Value;
    }

    public IActionResult Index()
    {
        // 方式1:用ViewBag传递
        ViewBag.AddressLine1 = _addressSettings.AddressLine1;
        ViewBag.AddressLine3 = _config["AddressLine3"];

        // 方式2:传递强类型模型到视图
        return View(_addressSettings);
    }
}
  1. 在视图中使用:
<!-- ViewBag方式 -->
<p>@ViewBag.AddressLine1</p>
<p>@ViewBag.AddressLine3</p>

<!-- 强类型模型方式(需指定模型) -->
@model YourProject.Models.AddressSettings
<p>@Model.AddressLine1</p>

注意:静态HTML文件无法直接读取配置

如果你的文件是纯.html静态文件,需要通过后端API接口返回配置,再用JavaScript获取:

  1. 新建API接口返回配置:
[ApiController]
[Route("api/config")]
public class ConfigController : ControllerBase
{
    private readonly IConfiguration _config;
    private readonly AddressSettings _addressSettings;

    public ConfigController(IConfiguration config, IOptions<AddressSettings> addressOptions)
    {
        _config = config;
        _addressSettings = addressOptions.Value;
    }

    [HttpGet("address")]
    public IActionResult GetAddressConfig()
    {
        return Ok(new
        {
            AddressLine1 = _addressSettings.AddressLine1,
            AddressLine2 = _addressSettings.AddressLine2,
            AddressLine3 = _config["AddressLine3"]
        });
    }
}
  1. 在静态HTML中用JS请求接口:
<p id="addressLine1"></p>
<p id="addressLine3"></p>

<script>
fetch('/api/config/address')
    .then(res => res.json())
    .then(data => {
        document.getElementById('addressLine1').textContent = data.AddressLine1;
        document.getElementById('addressLine3').textContent = data.AddressLine3;
    });
</script>

内容的提问来源于stack exchange,提问作者Kavita Korgaonkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 11:33:23