Asp.net mvc 在子网站中使用Postal和Hangfire
我一直在尝试在我的MVC5网站上使用Postal。当我将我的网页托管到子网站ie时,我收到了错误消息Asp.net mvc 在子网站中使用Postal和Hangfire,asp.net-mvc,asp.net-mvc-5,hangfire,postal,Asp.net Mvc,Asp.net Mvc 5,Hangfire,Postal,我一直在尝试在我的MVC5网站上使用Postal。当我将我的网页托管到子网站ie时,我收到了错误消息 虚拟路径“/”映射到另一个应用程序,这是不允许的 我已经调试到创建ControllerContext时,HttpContext设置不正确。因为我是从Hangfire运行Postal,所以HttpContext.Current总是空的。Postal使用下面的代码创建ContollerContext ControllerContext CreateControllerContex
- 虚拟路径“/”映射到另一个应用程序,这是不允许的
ControllerContext CreateControllerContext()
{
// A dummy HttpContextBase that is enough to allow the view to be rendered.
var httpContext = new HttpContextWrapper(
new HttpContext(
new HttpRequest("", UrlRoot(), ""),
new HttpResponse(TextWriter.Null)
)
);
var routeData = new RouteData();
routeData.Values["controller"] = EmailViewDirectoryName;
var requestContext = new RequestContext(httpContext, routeData);
var stubController = new StubController();
var controllerContext = new ControllerContext(requestContext, stubController);
stubController.ControllerContext = controllerContext;
return controllerContext;
}
string UrlRoot()
{
var httpContext = HttpContext.Current;
if (httpContext == null)
{
return "http://localhost";
}
return httpContext.Request.Url.GetLeftPart(UriPartial.Authority) +
httpContext.Request.ApplicationPath;
}
我怎样才能指定UrlRoot,而不是使用默认的localhost来根据我的子网站进行拉取?我按照这里的说明发送电子邮件。教程中的方法如下所示
public static void NotifyNewComment(int commentId)
{
// Prepare Postal classes to work outside of ASP.NET request
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
var emailService = new EmailService(engines);
// Get comment and send a notification.
using (var db = new MailerDbContext())
{
var comment = db.Comments.Find(commentId);
var email = new NewCommentEmail
{
To = "yourmail@example.com",
UserName = comment.UserName,
Comment = comment.Text
};
emailService.Send(email);
}
}
我发现问题是FileSystemRazorViewEngine没有被邮政部门使用。为了使这项工作正常进行,我必须确保FileSystemRazorViewEngine是可用的系统中的第一个引擎。然后我删除了它,因为我不希望它成为默认引擎。下面是我更新的方法
public static void NotifyNewComment(int commentId)
{
// Prepare Postal classes to work outside of ASP.NET request
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var eng = new FileSystemRazorViewEngine(viewsPath));
ViewEngines.Engines.Insert(0, eng);
var emailService = new EmailService(engines);
// Get comment and send a notification.
using (var db = new MailerDbContext())
{
var comment = db.Comments.Find(commentId);
var email = new NewCommentEmail
{
To = "yourmail@example.com",
UserName = comment.UserName,
Comment = comment.Text
};
emailService.Send(email);
ViewEngines.Engines.RemoveAt(0)
}
}
下面是另一个可能的解决方案,我认为比上面更优雅。它还解决了在执行后台进程时访问MVC应用程序时出现的问题
public static void SendTypedEmailBackground()
{
try
{
var engines = new ViewEngineCollection();
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var eng = new FileSystemRazorViewEngine(viewsPath);
engines.Add(eng);
var email = new WebApplication1.Controllers.EmailController.TypedEmail();
email.Date = DateTime.UtcNow.ToString();
IEmailService service = new Postal.EmailService(engines);
service.Send(email);
}
catch(Exception ex)
{
throw ex;
}
}
谢谢。不过我得到了一个错误(无法将null转换为'bool',因为它是不可为null的值类型)。你知道为什么会这样吗?