2
I have a solution with 4 projects inside. In my controller I need to open an HTML that is inside another project, but I can’t open the file folder.
Below follows print with my structure:
The class file is Utils.Cs the controller is within 1 - Services/JWT/Controllers I tried to open the file using the following code...
Criei uma classe no projeto onde está meu arquivo HTML.
public static class UtilDirectory
{
public static string GetDirectory()
{
return Directory.GetCurrentDirectory();
}
}
In the controller where I do the access class instance as follows:
var directory = UtilDirectory.GetDirectory();
StreamReader reader = new StreamReader(directory + @"/ApiUtils/HtmlEmail/CorpoEmailBoletoDimensao.html");
string mensagem = reader.ReadToEnd();
reader.Close();
But when I do it this way I end up getting the folder of my project that is the controller and not the Html itself.
Can you help me please?
I think this little tutorial will help you. https://www.dotnetperls.com/directory-getfiles
– Netinho Santos
Which way is coming in
directory
? if you call the methodGetDirectory()
from within acontroller
, it will return the directory of thecontroller
– Barbetta
Due to security restrictions, it is not very recommended to access files outside the scope of a web project directory. Since this file is from another project, could make an API that returns it, instead of searching in the Archive.
– Tony
@Barbetta Exactly, it returns me the path of controller, and because of that I can’t open the Html file.
– Lucas Brogni
@Tony I’ll try to do it this way.
– Lucas Brogni
If it works, you tell me that I put that comment of mine as an answer, OK?
– Tony
The correct thing would be for the Htmlemail folder to be in the web application structure or anywhere you want to leave it on the server, but setting up its path in appsettings.json and ensuring that the user running the application has access to the path and files.
– Leandro Angelo
@Leandroangelo can give me an example of how to configure the path in appsettings.json ? : S
– Lucas Brogni