簡體   English   中英

在已發布主機上引用 Blazor 服務器端中的靜態文件

[英]Reference Static Files in Blazor Server Side When on Published Host

在 Blazor 服務器上,如何在發布到 Web(托管)net5.0 時訪問目錄中的文件。 我有一個名為 Templates 的文件夾,我需要將一個 cshtlm 文件與之合並。 例如,這在本地有效,但在發布時失敗“找不到文件”。 如果它不在 www 文件夾中,我是否需要使用控制器? 新人開始學習。

   public async Task SendEmailAsync()
{

    string contentRootPath = Environment.ContentRootPath;
    var templateDir = Path.Combine(contentRootPath, @"Templates") ;
    var templateFilePath = Path.Combine(templateDir, "CleanDeskPolicy.cshtml");

    try
    {
        var vm = new NewAccountVm()
        {
            Username = "John Doe",
            Message = "Thank you for signing up with us!"
        };

        await EmailFactory.Create()
            .To("XXXXXX@gmail.com")
            .Subject("New Account")
            **.UsingTemplateFromFile(templateFilePath, vm)**
            .SendAsync();

        _logs.Add($"{DateTime.Now}: Email notification was sent successfully 📧✔!");
    }
    catch (Exception ex)
    {
        _logs.Add($"{DateTime.Now}: Failed to send email notification ❌! ({ex.Message})");
    }
}

您需要將文件放在 wwwroot 文件夾中

我使用 IWebHostEnvironment 來獲取我的文件的文件路徑 - 這在本地和我發布的應用程序中都有效。 我的模板文件夾在 wwwroot 內。 在每個模板文件的屬性中,我將內置操作設置為“內容”。

 public class MailService : IMailService
    {
        private readonly MailSettings _mailSettings;
    private readonly IWebHostEnvironment _webHostEnvironment;
    public MailService(IOptions<MailSettings> mailSettings, IWebHostEnvironment webHostEnvironment)
    {
        _mailSettings = mailSettings.Value;
        _webHostEnvironment = webHostEnvironment;
    }

    public async Task SendResetPasswordEmailAsync(MailRequest mailRequest, string callBackURL)
    {
        string FilePath = _webHostEnvironment.WebRootPath + "\\templates\\ResetPassword.html";
        StreamReader str = new StreamReader(FilePath);
        string MailText = str.ReadToEnd();
        str.Close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM