[英]How to create a wwwroot folder in .Net Core+React project?
I have created a.Net Core WebAPI project with a React template.我已经使用 React 模板创建了一个 .Net Core WebAPI 项目。 Now I want to send an email from the API with an email template.现在我想用 email 模板从 API 发送一个 email。 The template is in Html format so we need to read the Html file as a template from the root path but the 'wwwroot' path is not available in this project by default.模板是 Html 格式,所以我们需要从根路径读取 Html 文件作为模板,但是'wwwroot'路径在这个项目中默认是不可用的。
I have done this in my previous.Net Core MVC project as shown below but I don't know how to do this in the current project due to there is no 'WWWRoot' folder.我已经在我以前的 .Net Core MVC 项目中完成了此操作,如下所示,但由于没有“WWWRoot”文件夹,我不知道如何在当前项目中执行此操作。 Could anyone please help me that where to store this template file and how to retrieve it as a string in c#?谁能帮我把这个模板文件存储在哪里以及如何在 c# 中将其作为字符串检索?
//Get the email template path
string templatePath = RuntimeConfig.SiteUrl + "/Template/EmailTemplate.html";
//Adding the template to the email body
using (var client = new WebClient())
{
string reader = client.DownloadString(templatePath);
StringBuilder sb = new StringBuilder();
sb.Append(reader);
....
.... --logics---
}
I want to send an email from the API with an email template.我想使用 email 模板从 API 发送一个 email。
In my view, email template file(s) should not serve directly to clients.在我看来,email 模板文件不应直接提供给客户。
To achieve your requirement, you can try:为了达到您的要求,您可以尝试:
Create a folder to store your email template files创建一个文件夹来存储您的 email 模板文件
To access/read template file from that folder within controller action在 controller 操作中访问/读取该文件夹中的模板文件
public class EmailController : ControllerBase
{
private IWebHostEnvironment _env;
public EmailController(IWebHostEnvironment env)
{
_env = env;
}
[HttpPost]
public IActionResult SendConfirmEmail()
{
var path= Path.Combine(_env.ContentRootPath, @"EmailTemplates\" + "ConfirmEmailTemplate.html");
var template = System.IO.File.ReadAllText(path);
//code logic here
//...
//other actions
//...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.