ASP.NET核心-无法在控制器内使用C#StreamReader读取静态HTML文件
我有一个asp.net的核心项目与以下文件结构的角度模板 我想从Template文件夹中读取静态HTML文件(EmailConfirmation.HTML) 我的代码在控制器中读取此文件,如下所示ASP.NET核心-无法在控制器内使用C#StreamReader读取静态HTML文件,c#,angular,asp.net-core-2.1,C#,Angular,Asp.net Core 2.1,我有一个asp.net的核心项目与以下文件结构的角度模板 我想从Template文件夹中读取静态HTML文件(EmailConfirmation.HTML) 我的代码在控制器中读取此文件,如下所示 string body = string.Empty; using (StreamReader reader = new StreamReader("./Template/EmailConfirmation.html")) { body = reader.ReadToEnd(); } 但是当我将此
string body = string.Empty;
using (StreamReader reader = new StreamReader("./Template/EmailConfirmation.html"))
{
body = reader.ReadToEnd();
}
但是当我将此应用发布到azure时,在wwwroot文件夹中没有创建名为Template的文件夹。这样就犯了一个错误
出现错误:System.IO.DirectoryNotFoundException:无法
找到路径的一部分
'D:\home\site\wwwroot\Template\EmailConfirmation.html'
请注意:我已经尝试了下面文章中提到的步骤
但是没有成功
我怎样才能解决这个问题。谢谢你的关注。我期待着您的回复。Stratup.cs
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new
PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Templete")),
RequestPath = new PathString("/Templete")
});
}
控制器动作
string body = string.Empty;
//using streamreader for reading my htmltemplate
using (StreamReader reader = new StreamReader(Path.Combine("Templetes", "EmailConfirmation.html")))
{
body = reader.ReadToEnd();
}
Stratup.cs
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new
PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Templete")),
RequestPath = new PathString("/Templete")
});
}
控制器动作
string body = string.Empty;
//using streamreader for reading my htmltemplate
using (StreamReader reader = new StreamReader(Path.Combine("Templetes", "EmailConfirmation.html")))
{
body = reader.ReadToEnd();
}
试试这个:
using (var reader = File.OpenText(Path.Combine(@"./templates", $"{templateFileName}.html")))
{
return reader.ReadToEnd();
}
试试这个:
using (var reader = File.OpenText(Path.Combine(@"./templates", $"{templateFileName}.html")))
{
return reader.ReadToEnd();
}
您需要在项目文件中包含该文件,以便生成将其复制到输出文件夹。类似这样:将您的
模板
文件夹放到wwwroot
文件夹中,它应该可以从那里正常工作,或者按照教程进行配置。我也尝试过同样的方法。但未工作。您需要将该文件包含在项目文件中,以便生成将其复制到输出文件夹。类似这样:将您的模板
文件夹放到wwwroot
文件夹中,它应该可以从那里正常工作,或者按照教程进行配置。我也尝试过同样的方法。但不起作用。