2014年3月5日 星期三

將 mvc 應用程式發佈到正式環境後當登入成功時顯示首頁卻出現錯誤訊息

錯誤訊息可能是以下幾種之一
HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.
[NullReferenceException: Object reference not set to an instance of an object.]
   System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +30

錯誤會發生在 return RedirectToAction("Index", "Home") 這種時候

解法:
需要到以下網址安裝hotfix 才能避免遇到此問題,這個問題在開發環境內建的 iis express 並不會遇到,但是在正式發布的 iis 環境就會出現
http://www.microsoft.com/zh-TW/download/details.aspx?displaylang=en&id=5272

Entity Framework 建立新物件並儲存後馬上取得關聯資料

使用 CreateProxy 建立物件,不要直接 new var newmodel = _contextXXX.CreateProxy<yyy>(); ... _contextXXX.yyy.Add(newmodel); await _contextXXX.SaveC...