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

编程入门 行业动态 更新时间:2024-10-26 23:34:52
本文介绍了如何使用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?

我正在使用带有Redux入门版的Reactjs和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-14 02:41:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586003.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   数据   net   asp   core

发布评论

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

>www.elefans.com

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