1

I am absolutely new to ASP.NET developmentent and have been working through some tutorials since now.

One thing I am missing is a java like way to externalize text to files, which can contain variables. In java I am using velocity for such a purpose.

I need this to for an user specific email.

In java I would load a a text file like this:

Hello Mr. $name,
Thanks for your registration. Your customer id is: $customerid
....

Now I I am able load the text file via velocity and replace the given variables very easyly.

Is there any way to do this in ASP.Net? If not, what would be the proper way to do this?

Alexander
  • 7,178
  • 8
  • 45
  • 75

1 Answers1

0

You could define your email template in an MVC View, then render the View to a string, using the Model to provide the replacements. See the 'Razor .cshtml style' part of the answer to this post for the technical details of rendering a View to a string.

Community
  • 1
  • 1
Netricity
  • 2,550
  • 1
  • 22
  • 28
  • I am using the WebApi and not ASP.NET MVC. So it seems that there is no method to do this within the webapi? – Alexander May 06 '13 at 16:00
  • It's very achievable and acceptable to have MVC Views in a Web API project. You'd put the View under the usual /Views/ folder. Have you tried out the code sample in the link I posted? – Netricity May 07 '13 at 22:03
  • Yes I've tried that, but the problem is the following: I need to read this string within an WebAPI Controller. So I would have to implement a MVC Controller for the usage of the razor engine. Due to the fact, that I am handling a call on the webapi controller, I have no link to the MVC controller generating the view you mentioned via the razor engine from your link. I could instantiate the MVC Controller manually, yes, but then the ControllerContext - which is needed by razor - is null – Alexander May 08 '13 at 15:02
  • Have you seen the [MvcMailer](https://github.com/smsohan/MvcMailer) package? I don't know if it works directly with just Web API projects though. However, it's worth a look. To install it via Visual Studio Package Manager console: ***PM> install-package MvcMailer*** – Netricity May 10 '13 at 10:54