我正在使用郵政發送訂單確認。這是一個MVC4項目。該代碼正確地發送電子郵件確認。當使用郵政郵件MVC 4剃鬚刀移動視圖引擎生成錯誤
最近,我添加了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
看來,當郵政正在生成視圖,它擊中在Global.asax文件中的Application_Start,因爲用戶代理屬性爲null - 它產生的誤差。我試着在application_start過程中的特定代碼周圍放置一個try catch塊 - 但這不起作用。
我需要幫助如何告訴MVC或郵政不要打擾displaymodes。有什麼建議麼?