我有一个带有 Angular 模板的 asp.net core 项目,其文件结构如下。
我想从 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/