开发者

ASP.NET MVC Read files from server

I have 3 questions:

  1. 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/.
  2. How do开发者_StackOverflow社区 I unit test this? Should I even unit test reading files from the file system?
  3. Best way to read the file into a string?


  1. I would make a views folder for them /Views/Emails perhaps
  2. Unit test code that you write, not code from the .NET framework imo
  3. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜