c# - ASP.NET Core - 无法在 Controller 内使用 C# StreamReader 读取静态 HTML 文件

标签 c# angular asp.net-core-2.1

我有一个带有 Angular 模板的 asp.net core 项目,其文件结构如下。

enter image description here

我想从 Template 文件夹中读取静态 HTML 文件 (EmailConfirmation.html)。

我在 Controller 内读取此文件的代码如下。

string body = string.Empty;
using (StreamReader reader = new StreamReader("./Template/EmailConfirmation.html"))
{
body = reader.ReadToEnd();
}

但是当我将此应用程序发布到azure时,wwwroot文件夹内没有创建名为Template的文件夹。因此出现错误

Something went wrong: System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\home\site\wwwroot\Template\EmailConfirmation.html'

请注意:我已尝试使用下面文章中提到的步骤

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2

但是没有成功!

我该如何解决这个问题。感谢您的关注。我期待您的回复。

最佳答案

Stratup.cs

public void Configure(IApplicationBuilder app)
{
     app.UseStaticFiles(new StaticFileOptions()
     {
        FileProvider = new 
        PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Templete")),
                RequestPath = new PathString("/Templete")
     });
}

Controller 操作

string body = string.Empty;
//using streamreader for reading my htmltemplate   
using (StreamReader reader = new StreamReader(Path.Combine("Templetes", "EmailConfirmation.html")))
{
   body = reader.ReadToEnd();
}

关于c# - ASP.NET Core - 无法在 Controller 内使用 C# StreamReader 读取静态 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57113786/

相关文章:

c# - 派生类中的无意基类构造函数 "hiding"

c# - 为什么调用 Dispose?

c# - Entity Framework Core InMemory 数据库测试在并行运行时会中断

c# - 在二维网格中查找所有循环/封闭形状

Angular Testing - 如果计时器在组件初始化中,则 Tick 不起作用

javascript - Angular2 5 分钟安装错误 - 未定义要求

c# - 基于 .Net Core 中的 appSettings 使用 Cors

c# - 使用 asp.net core 2.1 下载角度 8 的文件总是给出损坏的文件

c# - ASP.Net Core 2.1 上的 Azure 移动后端连接问题

angular - tslint 错误阴影名称 : 'Observable'