我正尝试在web上使用MvcMailer,但我被卡住了!我得到了以下错误。我认为这与usermailer和view之间的路由有关。我发现它找不到视图,但我可能错了。任何帮助都将不胜感激。
谢谢
错误:
Value cannot be null.Parameter name: routeDataStackTrace:
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控制器:
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类:
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");
});
}
}文件夹结构:

谢谢
发布于 2015-12-08 19:52:05
我也用一个WebAPI2项目发送的邮件解决了这个问题-这个项目也有MVC引用,但只有在我的一个API控制器中有一个特定的邮件程序。我在另一个不同的API控制器中有另一个邮件发送程序,没有任何问题。
但我只是简单地补充说:
this.ControllerContext = new System.Web.Mvc.ControllerContext(this.CurrentHttpContext, new System.Web.Routing.RouteData(), this);到邮件构造函数中,现在一切都很好了。在这个特定的实例中,我假设上下文/routedata正在丢失-可能是因为我在这个特定方法上的属性路由的格式-因为工作的API方法(发送邮件)具有稍微不同的uri。但我可能离得太远了
也许有人能解释一下到底是怎么回事..
编辑:我稍微修改了路由和low,看起来这个方法运行得很好。是控制器上的[RoutePrefix("api/bookings")],然后
[Route("{ref}/sendmail")]
public async Task<IHttpActionResult> SendMail(string ref)关于不起作用的方法
但是如果我从路由中删除参数并执行
[Route("sendmail")]
public async Task<IHttpActionResult> SendMail(string ref)从querystring值获取参数-它工作得很好
发布于 2014-07-10 01:21:34
我不认为是视图没有被发现。我成功地使用了这一点,虽然你的代码与我的略有不同,但我看到的不同之处在于你的控制器。
在最上面的声明中,我有:
private IUserMailer _UserMailer = new UserMailer();
public IUserMailer UserMailer
{
get { return _UserMailer; }
set { _UserMailer = value; }
}我已经更新了这个,以反映您的邮件名称。希望这能有所帮助。
https://stackoverflow.com/questions/24654626
复制相似问题