2

I have a asp.net core project with angular template with below files structure.

enter image description here

I want to read the static HTML file (EmailConfirmation.html) from Template folder.

My code to read this file inside a controller as below.

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

But when I publish this app to azure, There is no folder created with the name Template inside wwwroot folder. Thus caught with an error

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

For your note : I have tried with steps mentioned in the below article

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

But not worked out !

How can I solve this problem. Thanks for your attention. I’m looking forward to your reply.

3
  • 1
    You need to include the file in your project file for the build to copy it to your output folder. Something like this: stackoverflow.com/questions/54762744/… Commented Jul 19, 2019 at 13:47
  • 1
    Drop your Template folder into the wwwroot folder instead and it should work fine from there or configure as per tutorial. Commented Jul 19, 2019 at 14:05
  • I tried same. But not worked. Commented Jul 22, 2019 at 12:08

2 Answers 2

3

Stratup.cs

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

Controller Action

string body = string.Empty;
//using streamreader for reading my htmltemplate   
using (StreamReader reader = new StreamReader(Path.Combine("Templetes", "EmailConfirmation.html")))
{
   body = reader.ReadToEnd();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

using (var reader = File.OpenText(Path.Combine(@"./templates", $"{templateFileName}.html")))
        {
            return reader.ReadToEnd();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.