简体   繁体   English

托管 asp.net 应用程序 DirectoryNotFoundException

[英]Hosted asp.net application DirectoryNotFoundException

I have an application running on my local server.我有一个应用程序在我的本地服务器上运行。 The application runs locally without any error.该应用程序在本地运行,没有任何错误。 But, when I host the application in the Heroku .但是,当我在Heroku中托管应用程序时。 It produces the error as:它产生的错误为:

DirectoryNotFoundException: Could not find a part of the path'/app/heroku_output/EmailTemplate/EmailConfirm.html'.

I have been trying to read the file using the below code:我一直在尝试使用以下代码读取文件:

private const string templatePath = @"EmailTemplate/{0}.html";

private string GetEmailBody(string templateName)
{
    var body = File.ReadAllText(string.Format(templatePath, templateName));
    return body;
}

The folder EmailTemplate is in my root directory as shown in the figure.如图所示,文件夹EmailTemplate在我的根目录中。

在此处输入图像描述

That folder has also been uploaded to Github and hosted properly in the Heroku.该文件夹也已上传到 Github 并正确托管在 Heroku 中。 But the application is looking at the file in the wrong project directory ie in (/app/heroku_output).但是应用程序正在查看错误项目目录中的文件,即 (/app/heroku_output)。

What am I missing in my code?我的代码中缺少什么? How do I approach to read those directories?我该如何阅读这些目录?

After looking for all the deployed files,查找所有已部署的文件后,

You attempt to read a file.您尝试读取文件。 Reading files is possible within the wwwroot folder.可以在wwwroot文件夹中读取文件。 So move your templates there.所以把你的模板移到那里。 To access the wwwroot folder use the WebRootPath property from object injected as IWebHostEnvironment要访问wwwroot文件夹,请使用作为 IWebHostEnvironment 注入的IWebHostEnvironment中的WebRootPath属性

public class EmailTemplateGenerator
{
    private IWebHostEnvironment _env;
    public EmailTemplateGenerator(IWebHostEnvironment env)
    {
        _env = env;
    }

    public void GetEmailBody(string templateName)
    {
        var template = File.OpenRead(Path.Combine(_env.WebRootPath, $"EmailTemplate/{templateName}.html")
    }
}

Try following:尝试以下操作:

In Visual Studio在 Visual Studio 中

If you are using Visual Studio to publish, right click each of those files under "EmailTemplate" folder and select "Properties" and set value of "Copy to Output Directory" to "Copy Always".如果您使用 Visual Studio 发布,请右键单击“EmailTemplate”文件夹和 select“属性”下的每个文件,并将“复制到 Output 目录”的值设置为“始终复制”。 This will make Visual Studio to publish these files to hosted environment.这将使 Visual Studio 将这些文件发布到托管环境。 If you are using different application to publish files just make sure "EmailTemplate" folder is published to hosted environment.如果您使用不同的应用程序发布文件,只需确保将“EmailTemplate”文件夹发布到托管环境。

In Code在代码中

To be on the safe side always access your files through base URL.为了安全起见,请始终通过基础 URL 访问您的文件。 Example is given below.下面给出示例。

//private const string templatePath = @"EmailTemplate/{0}.html";
private const string templatePath = @"EmailTemplate\\{0}.html";

private string GetEmailBody(string templateName)
{
    //var body = File.ReadAllText(string.Format(templatePath, templateName));
    string baseURL = AppDomain.CurrentDomain.BaseDirectory;
    var body = File.ReadAllText(string.Format(baseURL + "\\" + templatePath, templateName));
    return body;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM