我看到了一個相關的問題: Sitecore Glass Mapper always nullSitecore的玻璃映射的GetItem <TypeName>(GUID)總是返回null
不過遺憾的是它並沒有給我的情況的解決方案。
這裏去的代碼片段:
var db = Factory.GetDatabase("master");
var context = new SitecoreContext();
// the ID of Needed item
var g = new Guid("{F21C04FE-8826-41AB-9F3C-F7BDF5B35C76}");
// just to test if it's possible to fetch item using db.GetItem
var i = db.GetItem(new ID(g), Language.Current, Sitecore.Data.Version.Latest);
// Grab item
var t = context.GetItem<Article>(g);
在上面的代碼: 我不爲空 t爲空
文章是簡單的類,如:
[SitecoreType(TemplateId = "{4C4EC1DA-EB77-4001-A7F9-E4C2F61A9BE9}")]
public class Article
{
[SitecoreField(FieldName = "Title")]
public string Title { get; set; }
}
有隻有一種語言安裝在Sitecore - en中,它也已在web.config中的項目中指定。
而且我已經在Global.asax.cs
添加GlassMapperSc.Start();
到Application_Start
,並通過添加var attributes = new AttributeConfigurationLoader(new[] { "Assembly.Name" });
我的組件包括的組件列表中,我成功地找到我的課在SitecoreContext
映射。
它看起來不像語言問題,正如開頭提供的鏈接所述。我已經在相當長的時間裏苦苦掙扎,但沒有運氣......
謝謝!
只是可以肯定,你檢查你的項目'{F21C04FE-8826-41AB-9F3C-F7BDF5B35C76} '有一個有效的版本? –
使用標準的Sitecore API獲取項目並檢查它是否是您期望的語言,並且還有版本。我發現問題在哪裏(並且沒有理由),SC正在使用與您認爲應該使用的語言不同的語言。 –
@ michael-edwards我檢查過它,它有langage'en'和版本'1'。 –