0

I am trying to use MvcMailer on a web api and i am stuck!!! I am getting the following error. I think it has something to do with the routing between usermailer and view. I seen it can't find the view, but i could be wrong. Any help would be appreciate.

Thanks

Error:

Value cannot be null.Parameter name: routeData

StackTrace:

at System.Web.Routing.RequestContext..ctor(HttpContextBase httpContext, RouteData routeData)
   at System.Web.Mvc.ControllerContext..ctor(HttpContextBase httpContext, RouteData routeData, ControllerBase controller)
   at Mvc.Mailer.MailerBase.CreateControllerContext()
   at Mvc.Mailer.MailerBase.ViewExists(String viewName, String masterName)
   at Mvc.Mailer.MailerBase.TextViewExists(String viewName, String masterName)
   at Mvc.Mailer.MailerBase.PopulateBody(MailMessage mailMessage, String viewName, String masterName, Dictionary`2 linkedResources)
   at Mvc.Mailer.MailerBase.Populate(Action`1 action)
   at App.Api.Mailers.UserMailer.NewCandidate() in e:\VS2013 Projects\App.Api\Mailers\UserMailer.cs:line 15
   at App.Api.Controllers.CandidatesController.Get() in e:\VS2013 Projects\App.Api\Controllers\CandidatesController.cs:line 31

controller:

    private readonly UserMailer _mailer = new UserMailer();      

    [Authorize]
    [Route("")]
    [HttpPost]
    // POST api/requests
    public HttpResponseMessage Post(Candidate candidate)
    {
        _repository.CandidateRepository.Insert(candidate);
        _repository.Save();
        _mailer.NewCandidate().Send();
        return Request.CreateResponse(HttpStatusCode.OK, candidate);

    }

UserMailer Class:

 public class UserMailer : MailerBase, IUserMailer  
{
    public UserMailer()
    {
        MasterName="_Layout";
    }

    public virtual MvcMailMessage NewCandidate()
    {
        //ViewBag.Data = someObject;
        return Populate(x =>
        {
            x.Subject = "NewCandidate";
            x.ViewName = "NewCandidate";
            x.To.Add("test@test.test");
        });
    }
}

Folder Structure:

Thanks

Valter
  • 2,859
  • 5
  • 30
  • 51

2 Answers2

1

I have also hit this issue with a mail being sent form a WebAPI2 project - that also has the MVC references, but only with a specific mailer in one of my API controllers. I have another mailer in a different API controller that has no problems at all.

but I simply added :

    this.ControllerContext = new System.Web.Mvc.ControllerContext(this.CurrentHttpContext, new System.Web.Routing.RouteData(), this);

into the mailer constructor and it all works fine now. I presume in this particular instance that the context/routedata is getting lost - possibly because of the format of my attribute routing on this particular method - as the API methods that work (with sending mails) have a slightly different uri. but i may be way off the mark there

maybe someone could explain what is really going on ..

EDIT: I slightly altered the route and low and behold the method worked fine. was [RoutePrefix("api/bookings")] on the controller and then

[Route("{ref}/sendmail")]
public async Task<IHttpActionResult> SendMail(string ref)

on the method which didn't work

but if i remove the parameter from the route and do

[Route("sendmail")]
public async Task<IHttpActionResult> SendMail(string ref)

getting the param from a querystring value - it works fine

nat
  • 2,185
  • 5
  • 32
  • 64
0

I don't think its the view being not found. I use this successfully and whilst your code is slightly different to mine, the difference I see is in your controller.

At the top declaration I have:

    private IUserMailer _UserMailer = new UserMailer();
    public IUserMailer UserMailer
    {
        get { return _UserMailer; }
        set { _UserMailer = value; }
    }

I have updated this to reflect your mailer name. Hope this helps.

Steve Newton
  • 1,046
  • 1
  • 11
  • 28
  • Didn't worked, getting same error. I read online that i have to install package EmbeddedResourceVirtualPathProvider. Did you install this package? Did you install any addition package? – Valter Jul 09 '14 at 17:39
  • No, I don't have that. What folder have you placed your views in or did you use the scaffolder? I will edit my answer to show you how I create the mailer as it is different and may help you. – Steve Newton Jul 09 '14 at 17:58
  • Edit the question: add folder structure and yes i used scaffolder – Valter Jul 09 '14 at 18:26
  • Do you have a MVC4/5 with web api project? I only have web api project, but i have the system.web.mvc and system.web.razor as reference. Just wondering if the project is set up as web api is causing the issue, but not sure how to fix it. – Valter Jul 09 '14 at 18:40
  • Ah yes. MVC Mailer requires the HTTPContext so that is going to be the issue. Hacking anything would be a mistake as the next version is removing system.web as well. Sorry but past my knowledge now, you could ask on the project page as he is usually very helpful. Good luck. – Steve Newton Jul 09 '14 at 20:00