Reputation: 2899
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.
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("[email protected]");
});
}
}
Folder Structure:
Upvotes: 0
Views: 907
Reputation: 2197
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
Upvotes: 1
Reputation: 1078
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.
Upvotes: 0