我已尝试将其他解决方案发布到stackoverflow并且发现没有任何工作,这是我的问题.
所以我想通过我的MVC应用程序使用hangfire发送电子邮件,这可以在我的本地计算机上运行但是当我将它上传到远程服务器时,我在hangfire上收到以下错误:
The Virtual path '/' maps to another application, which is not allowed
这是我用来发送电子邮件的代码:
foreach (var EmailEntry in EmailEntries)
{
var email = new EmailTemplateModel
{
ViewName = "EmailTemplateModel",
FromAddress = "[email protected]",
EmailAddress = EmailEntry,
Subject = "Task Report",
Date = Dates,
Task = DatesAndTasks,
};
email.Send();
}
当我使用’ViewName’方法时,它在我的本地机器上返回’〜/ Views / Emails’.
在Send()方法内部:
// Summary:
// Convenience method that sends this email via a default EmailService.
public void Send();
IIS中的应用程序结构:
服务器>网站>默认>我的应用程序
JodyL的解决方案提出的问题如下:
StructureMapDependencyScope.get_CurrentNestedContainer()
解:
在“StructureMapDependencyScope”类中编辑了以下代码:
之前:
public IContainer CurrentNestedContainer {
get {
return (IContainer)HttpContext.Items[NestedContainerKey];
}
set {
HttpContext.Items[NestedContainerKey] = value;
}
}
后:
public IContainer CurrentNestedContainer {
get {
IContainer container = null;
if (HttpContext != null)
container = (IContainer)HttpContext.Items[NestedContainerKey];
return container;
}
set {
HttpContext.Items[NestedContainerKey] = value;
}
}
之前:
private HttpContextBase HttpContext {
get {
var ctx = Container.TryGetInstance();
return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current);
}
}
后:
private HttpContextBase HttpContext {
get {
var ctx = Container.TryGetInstance();
if (ctx == null && System.Web.HttpContext.Current == null)
return null;
return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current);
}
}
此问题可能是由于使用Hangfire时邮件无法使用HTTPContext引起的.如果HTTPContext.Current为null,则Postal使用
http://localhost作为URL,该URL不会映射到您的应用程序位置.为了解决此问题,您可以在发送电子邮件时使用FileSystemRazorViewEngine并将其传递给电子邮件模板的路径.
有关如何实现此目的的详细信息,请参阅此问题的答案.
Using Postal and Hangfire in Subsite