2

我一直在尝试在我的 MVC5 网站上使用 Postal。当我将网页托管一个子站点时,即http://localhost/Subsite我收到错误

  • 虚拟路径“/”映射到另一个应用程序,这是不允许的

我已将其调试到创建 ControllerContext 时,HttpContext 设置不正确。由于我从 Hangfire 运行 Postal,因此 HttpContext.Current 始终为空。Postal 使用下面的代码创建 ContollerContext。

        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 的默认值来拉取它?

4

2 回答 2

1

我按照这里的说明http://docs.hangfire.io/en/latest/tutorials/send-email.html发送我的电子邮件。教程中的方法如下

    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 没有被使用 bty postal。为了让它工作,我必须确保 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)
}
}
于 2016-02-15T22:36:42.803 回答
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;
    }
}
于 2016-04-08T23:15:11.330 回答