【问题标题】:How to create my own http context for a controller?如何为控制器创建我自己的 http 上下文?
【发布时间】:2011-05-18 19:20:59
【问题描述】:

我正在使用 asp.net mvc 3 和石英调度程序。

目前我有这个

  1. 创建并执行作业。
  2. 转到一个控制器,我在其中映射域以使用自动映射器查看模型
  3. 结果转到我想使用的控制器 Action Mailer

我收到一个错误

System.ArgumentNullException was unhandled by user code
  Message=Value cannot be null.
Parameter name: httpContext
  Source=System.Web
  ParamName=httpContext
  StackTrace:
       at System.Web.Routing.RequestContext..ctor(HttpContextBase httpContext, RouteData routeData)
       at ActionMailer.Net.MailerBase.Email(String viewName, Object model, String masterName)
       at EmailController.SendCalendarAppointmentNotifiation(CalendarAppointmentReminderVM vm) in EmailController.cs:line 73
       at RemindersController.CalendarAppointmentsReminders(List`1 taskReminders) in RemindersController.cs:line 54
       at QuartzJobs.AppointmentRemindersJob.Execute(JobExecutionContext context) in AppointmentRemindersJob.cs:line 39
       at Quartz.Core.JobRunShell.Run()
  InnerException: 

// 工作

public void Execute(JobExecutionContext context)
        {


                            // some code to do some checking and to get results above(not shown)
                            RemindersController remindersController = new RemindersController();
                            remindersController.CalendarAppointmentsReminders(calendarAppointmentReminders);

            }

//控制器(做映射)\

   public void CalendarAppointmentsReminders(List<AppointmentReminder> appointments)
        {
           List<CalendarAppointmentReminderVM> vm = Mapper.Map<List<CalendarAppointment>, List<CalendarAppointmentReminderVM>>(appointments.Select(x => x.CalendarAppointment).ToList());
            Mapper.Map<List<AppointmentReminder>, List<CalendarAppointmentReminderVM>>(appointments, vm);


            foreach (var v in vm)
            {
                new EmailController().SendCalendarAppointmentNotifiation(v);
            }

        }

// mvc 邮件程序

 public EmailResult SendCalendarAppointmentNotifiation(CalendarAppointmentReminderVM vm)
        {
            To.Add(vm.To);
            Subject = String.Format("Subject");
            return Email("SendCalendarAppointmentEmail", vm);
        }

死在return Email("SendCalendarAppointmentEmail", vm); 我想用它,因为我用它来发送我所有的其他电子邮件,我发现它发送电子邮件的方式要好得多(更容易让它们看起来更好,因为你可以使用母版页和强类型视图)。

【问题讨论】:

  • 您的 SMTP 设置是否正确?
  • @ Brian Driscoll - 设置到本地文件夹和我所有其他的工作(用户请求并联系我的控制器的那些我调用这个电子邮件控制器并发送电子邮件.. .so httpcontext 是在第一次调用控制器时为我制作的)
  • 这个问题解决了吗?请提供详细信息。谢谢
  • 我在 ActionMailer 上遇到了同样的问题,如果 HttpContext 不存在,它就不允许发送电子邮件。这是一个巨大的限制!

标签: c# asp.net-mvc asp.net-mvc-3 quartz-scheduler httpcontext


【解决方案1】:

Quartz 的 Job 执行不在 HttpRequest 中运行。 HttpContext.Current 也是 NULL。 如果没有 asp.net 请求环境,这个 asp.net 邮件程序似乎无法工作(看看 StackTrace)

checkout Render a view as a string(渲染视图,放入邮件并发送..),但不幸的是,在这篇文章中,所有示例也适用于当前的 ControllerContext/HttpContext。

【讨论】:

  • Müller - 是的 HttpContext 为空。我实际上把名字弄混了,我使用的是 ActionMailer 而不是 asp.net mvc 邮件程序。我认为这两个电子邮件发件人都遇到了同样的问题,他们需要 HttpContext。我迷失在第一步。那是在做什么?所以在我的第一个控制器中,我应该把第一步代码放进去?
  • 我用 mvc2 做到了这一点,在工作中通过电子邮件发送 asp.net 呈现的部分视图。我会把代码发给你
  • 如果我需要 httpContext,我该如何使用它?
猜你喜欢
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
  • 1970-01-01
  • 2012-03-11
  • 2018-10-22
相关资源
最近更新 更多