ASP.NET MVC Read files from server
I have 3 questions:
- Where is the correct place to put some template files? I'll be using these templates to render emails with DotLiquid. I'm thinking about just having it at
~/Templates/
. - How do开发者_StackOverflow社区 I unit test this? Should I even unit test reading files from the file system?
- Best way to read the file into a string?
- I would make a views folder for them
/Views/Emails
perhaps - Unit test code that you write, not code from the .NET framework imo
string s = System.IO.File.ReadAllText( path );
Check out this blog entry, which talks about how to send emails using a view as a template: ASP.NET MVC 2 Render Template to String.
In short, you create a method that renders the View into a string and then call that method from an action to generate the email body content.
Placing the code in ~/Content/Templates/ and downloading the content using a web client worked best for me.
var welcomeMailTemplatePath = "yourPath";
var webClient = new WebClient();
string html = webClient.DownloadString(WelcomeMailTemplatePath);
This way, you don't need to deal with controllers/views and can directly grab the contents of the template file.
精彩评论