简体   繁体   English

.Net Core+React项目如何创建wwwroot文件夹?

[英]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.

相关问题 如何上传附件到服务器,Net Core+React(Redux) - How to upload attachment to server ,Net Core+React(Redux) 如何更改 .NET Core 项目中“wwwroot”文件夹的图标? - How do you change the icon of the `wwwroot` folder in a .NET Core project? 更新到 Asp.Net Core 3 的 Identity 项目提供了 wwwroot 文件夹 - Identity project updated to Asp.Net Core 3 gives wwwroot folder 在ASP.NET 4.5项目中使用wwwroot文件夹(ASP.NET Core样式) - Using a wwwroot folder (ASP.NET Core style) in ASP.NET 4.5 project 在IIS .net核心wwwroot上找不到CSS文件夹 - CSS folder not found on IIS .net core wwwroot 将“react js”构建到我的“asp.net core web api”的 wwwroot 文件夹时出现路由问题 - Routing problem when building "react js" to wwwroot folder of my "asp.net core web api" 如何使用 controller 或在 ASP.NET Core MVC 中查看 wwwroot 文件夹中的 index.html 文件 - How to render index.html file that is in wwwroot folder using controller or view in ASP.NET Core MVC 文件上传到ASP.NET Core中的wwwroot文件夹 - File upload to wwwroot folder in ASP.NET Core 如何重命名ASP.NET 5 Web项目中用作Web根的“ wwwroot”文件夹 - How to rename “wwwroot” folder used as web root in ASP.NET 5 web project 如何访问不同项目的 wwwroot 文件夹中的文件? - How to access files within the wwwroot folder of a different project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM