4

I have tried other solutions posted to stackoverflow and have found none that work, here is my problem.

So I want to send an email using hangfire through my MVC application, this works on my local machine but when I upload it to a remote server I get the following error on hangfire:

The virtual path '/' maps to another application, which is not allowed

This is the code I use to send the email:

foreach (var EmailEntry in EmailEntries)
        {
            var email = new EmailTemplateModel
            {
                ViewName = "EmailTemplateModel",
                FromAddress = "donotreply@emailservice.com",
                EmailAddress = EmailEntry,
                Subject = "Task Report",
                Date = Dates,
                Task = DatesAndTasks,
            };
            email.Send();
        }

When I use the 'ViewName' method it returns '~/Views/Emails' on my local machine.

Inside of the Send() method:

// Summary:
        //     Convenience method that sends this email via a default EmailService.
        public void Send();

Application structure in IIS:

Server > Sites > Default > MyApplication

Issue raised by JodyL's solution below:

StructureMapDependencyScope.get_CurrentNestedContainer() 

enter image description here

Solution:

Edited the following code in 'StructureMapDependencyScope' class:

Before:

public IContainer CurrentNestedContainer {
            get {
                return (IContainer)HttpContext.Items[NestedContainerKey];
            }
            set {
                HttpContext.Items[NestedContainerKey] = value;
            }
        }

After:

public IContainer CurrentNestedContainer {
            get {
                IContainer container = null;
                if (HttpContext != null)
                    container = (IContainer)HttpContext.Items[NestedContainerKey];
                return container;
            }
            set {
                HttpContext.Items[NestedContainerKey] = value;
            }
        } 

Before:

private HttpContextBase HttpContext {
            get {
                var ctx = Container.TryGetInstance<HttpContextBase>();
                return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current);
            }
        }

After:

private HttpContextBase HttpContext {
            get {
                var ctx = Container.TryGetInstance<HttpContextBase>();
                if (ctx == null && System.Web.HttpContext.Current == null)
                    return null;

                return ctx ?? new HttpContextWrapper(System.Web.HttpContext.Current);
            }
        } 
4

3 回答 3

1

这个问题可能是因为使用 Hangfire 时 HTTPContext 对 Postal 不可用。如果 HTTPContext.Current 为 null,Postal 使用http://localhost作为 URL,它不会映射到您的应用程序位置。为了解决这个问题,您可以在发送电子邮件时使用 FileSystemRazorViewEngine 并将您的电子邮件模板的路径传递给它。

有关如何实现此目的的详细信息,请参阅此问题的答案。 在子站点中使用 Postal 和 Hangfire

于 2018-04-27T07:53:37.640 回答
0

很可能问题与 IIS 而不是 Hangfire 等有关。所以,请您尝试使用 with dot (.),如下所示:

而不是使用

'~/Views/Emails'

尝试这个

'./Views/Emails'

同样,您在显示图像时也可能会遇到一些问题,在这种情况下,您应该进行以下更改:

~/../Content/images/image.png

./../Content/images/image.png

希望这可以帮助...

于 2018-04-25T15:46:23.157 回答
0

请尝试MapPath如下使用:

var path = HostingEnvironment.MapPath(@"~/Views/Emails");
于 2018-04-26T10:32:07.077 回答