Question

I have hybrid project of ASP Web Api and angular project similar to this one and I need to send emails from ApiControllers in it.

I like MvcMailer and I'd prefer to use that. I managed it to actually send emails. However, I'm not sure how to pass custom data to the view since I do not have access to ViewBag or ViewData in ApiController context.

Any help is appreciated.

Was it helpful?

Solution

I understand you do not have access to ViewBag/ViewData, which is probably related that you have a WebAPI context (and not MVC). But why don't you just instantiate the mailer and pass parameters?

[RoutePrefix("api/foo")]
public class FooController : ApiController
{
    [HttpPost]
    [Route("bar")]
    public void Bar(string foo, string bar)
    {
        MyMailer myMailer = new MyMailer();
        myMailer.MyAction(foo, bar).Send();
    }
}

Then:

public class MyMailer : MailerBase, IMyMailer   
{
    public virtual MvcMailMessage MyAction(string foo, string bar)
    {
        ViewBag.Foo = foo;
        ViewBag.Bar = bar;
        // Populate ...
    }
}

I know it's not optimal but that's what I ended up doing and it works fine. Would that help?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top