如何使用 asp.net core & 从 appsettings.json 获取配置数据反应

编程入门 行业动态 更新时间:2024-10-25 04:16:54
本文介绍了如何使用 asp core & 从 appsettings.json 获取配置数据反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用带有 react/redux 的 asp 核心,并且我正在尝试将 react/redux 中的配置数据移动到 appsettings.json 文件中.但是一旦配置数据被移动;ClientApp 如何访问配置数据?

I'm using asp core with react/redux and i'm trying to move my config data that is in react/redux into the appsettings.json file. But once the config data has been moved; how is the config data accessible to the ClientApp?

我将 Reactjs 与 Redux starter 一起用于 ASP.NET Core.

I'm using the Reactjs with Redux starter for ASP.NET Core.

更新

如果这不是推荐的应用配置存储方式,请告诉我是什么.

If this is not the recommended way to store app configs please let me know what is.

推荐答案

我不建议从客户端读取配置文件.但是,如果您想这样做,我建议您创建一个服务来读取 json 文件并为客户端公开一些配置.

I would not recommend reading the config file from your client side. But if you want to do I would suggest you create a service that read the json file and expo some configuration for client side.

您可以像这样从 ASP.Net Core 端进行配置

You can config from ASP.Net Core side like this

例如你的 json 中有这样的配置

Example you have config like this in your json

"EmailSettings": { "MailServer": "", "MailPort": , "Email": "", "Password": "", "SenderName": "", "Sender": "", "SysAdminEmail": "" },

所以你需要用config定义匹配的模型

So you need to define the matching model with config

public class EmailSettings { public string MailServer { get; set; } public int MailPort { get; set; } public string SenderName { get; set; } public string Sender { get; set; } public string Email { get; set; } public string Password { get; set; } public string SysAdminEmail { get; set; } }

然后在 Startup.cs 中注册

Then register in Startup.cs

services.Configure<EmailSettings>(configuration.GetSection("EmailSettings"));

所以你可以在你的服务中使用

So you can use in your service

private readonly IOptions<EmailSettings> _emailSetting; public EmailSender(IOptions<EmailSettings> emailSetting) { _emailSetting = emailSetting; } public string GetConfig(){ return _emailSetting.Value.SenderName; }

更多推荐

如何使用 asp.net core &amp; 从 appsettings.json 获取配置数据反应

本文发布于:2023-11-02 15:03:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552713.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   数据   net   asp   core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!