【问题标题】:when using Postal mail MVC 4 Razor Mobile View Engine generates error使用 Postal mail MVC 4 Razor Mobile View Engine 时生成错误
【发布时间】:2014-04-16 13:26:57
【问题描述】:

我正在使用邮寄方式发送订单确认信息。这是一个 MVC4 项目。该代码正在正确发送电子邮件确认。

最近,我在项目中添加了 MVC 移动版。一切正常 - 除了当用户在移动设备上发送确认电子邮件时。这是订单确认类:

    public class OrderConfirmation : Postal.Email
    {
        public string email { get; set; }
        public string partyid { get; set; }
        public string GrouponCode { get; set; }
        public string originalpartyDate { get; set; }
        public string originalpartyStartTime { get; set; }
        public string originalpartyTitle { get; set; }
        public string originalOrderDate { get; set; }
        public bool GrouponWasUpgraded { get; set; }
        public decimal GrouponFaceValue { get; set; }
        public bool ClassCancelled { get; set; }
        public string firstname { get; set; }
        public string orderlink { get; set; }
    }

然后它被这样调用:

ci = new Email.OrderConfirmation
{
    ClassCancelled = false,
    email = Email,
    firstname = FirstName,
    orderlink = "https://website.com/Checkout/Archive?orderlinkid=" + OrderLinkId,
    originalpartyDate = DateStart.ToShortDateString(),
    originalpartyStartTime = DateStart.ToShortTimeString(),
    originalpartyTitle = odt.Party.Title,
    partyid = odt.PartyId.ToString()
 };
 Task task = ci.SendAsync();

在我的 Global.asax.cs 文件中,我检测移动设备,并插入显示模式,如下所示:

   protected void Application_Start()
   {
   DisplayModeProvider.Instance.Modes.Insert(0, new
        DefaultDisplayMode("Tablet")
        {
            ContextCondition = (ctx =>
            ctx.Request.UserAgent.IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0 ||
            ctx.Request.UserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0 &&
            ctx.Request.UserAgent.IndexOf("mobile", StringComparison.OrdinalIgnoreCase) < 1)
        });
        DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("iPhone")
            {
                ContextCondition = (ctx =>
                    ctx.GetOverriddenBrowser().IsMobileDevice ||
                    ctx.GetOverriddenUserAgent().IndexOf
                    ("iPhone", StringComparison.OrdinalIgnoreCase) >= 0
                    )
            });
     /// omitted the rest of the (standard) code for brevity
    }

我收到的错误消息是通用的“对象引用未设置为对象的实例”。 我在下面放了堆栈跟踪,以及生成错误代码时的图像。

iisexpress.exe Error: 0 : 4/15/2014 11:09:33 AMSystem.NullReferenceException: Object reference not set to an instance of an object.
   at application.MvcApplication.<Application_Start>b__1(HttpContextBase ctx) in c:\Dropbox\SourceCode\PUYF_NonTFS\PUYF_Website\Application\Global.asax.cs:line 132
   at System.Web.WebPages.DefaultDisplayMode.CanHandleContext(HttpContextBase httpContext)
   at System.Web.WebPages.DisplayModeProvider.<>c__DisplayClass6.<GetAvailableDisplayModesForContext>b__5(IDisplayMode mode)
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations)
   at System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache)
   at System.Web.Mvc.ViewEngineCollection.<>c__DisplayClassc.<FindView>b__a(IViewEngine e)
   at System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, Boolean trackSearchedPaths)
   at System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext controllerContext, String viewName, String masterName)
   at Postal.EmailViewRenderer.CreateView(String viewName, ControllerContext controllerContext)
   at Postal.EmailViewRenderer.Render(Email email, String viewName)
   at Postal.EmailService.CreateMailMessage(Email email)
   at Postal.EmailService.SendAsync(Email email)
   at Postal.Email.SendAsync()
   at Application.Models.Order.SendConfirmation(ArtStoreEntities dbcontext) in    
c:\Dropbox\SourceCode\PUYF_NonTFS\PUYF_Website\Application\Models\Order.cs:line 134

似乎,当 postal 生成视图时,它会点击 global.asax 文件中的 application_start,并且因为 useragent 属性为空 - 它会生成错误。我尝试在 application_start 过程中的特定代码周围放置一个 try catch 块——但这不起作用。

我需要帮助如何告诉 MVC 或 Postal 不要打扰显示模式。有什么建议吗?

【问题讨论】:

    标签: asp.net-mvc-4 email mobile razor postal


    【解决方案1】:

    这就是我最终做的:

        DisplayModeProvider.Instance.Modes.Insert(0, new
                DefaultDisplayMode("Tablet"){
            ContextCondition = (ctx =>
            ctx.Request.UserAgent != null &&
            (
            ctx.Request.UserAgent.IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0 ||
            ctx.Request.UserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0 &&
            ctx.Request.UserAgent.IndexOf("mobile", StringComparison.OrdinalIgnoreCase) < 1))
        });
    
        DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("iPhone"){
            ContextCondition = (ctx =>
                ctx.Request.UserAgent != null &&
            (
                ctx.GetOverriddenBrowser().IsMobileDevice ||
                ctx.GetOverriddenUserAgent().IndexOf
                ("iPhone", StringComparison.OrdinalIgnoreCase) >= 0
                )
            )
        });
    

    【讨论】:

      【解决方案2】:

      ctx.Request.UserAgent 为空时,您能否尝试使ContextCondition 返回false?

      【讨论】:

        猜你喜欢
        • 2018-06-11
        • 1970-01-01
        • 2013-07-20
        • 1970-01-01
        • 1970-01-01
        • 2015-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多