授权方 Class 中的 Orchard 上下文为空
Orchard Context Null in Authorizer Class
在 Authorizer
class 的第 72 行,我收到空引用异常:
if (_authorizationService.TryCheckAccess(permission, _workContextAccessor.GetContext().CurrentUser, content))
问题是 GetContext()
返回 null
所以,很明显,引用 CurrentUser
会给我带来问题。
最初,这是一个创建分类术语的迁移,导致对授权方的调用。事实上,完整的堆栈跟踪可以在 post.
下面找到
我已经尝试了重构迁移方面的所有方法,但它的代码已经在代码的其他地方被证明了 3 次(其中一次在迁移中 - 所以我确信 'context' 我这样做很好)。
迁移代码:
var taxonomy = _contentManager.New<TaxonomyPart>("Taxonomy");
taxonomy.Name = "Categories";
_contentManager.Create(taxonomy, VersionOptions.Published);
var parentTerm = _taxonomyService.NewTerm(taxonomy);
parentTerm.Name = "Products";
parentTerm.Container = taxonomy.ContentItem;
_contentManager.Create(parentTerm, VersionOptions.Published);
string[] childTermNames = { "Disks", "Mice", "Keyboards" };
foreach (var termName in childTermNames)
{
var term = _taxonomyService.NewTerm(taxonomy);
term.Name = termName;
term.Container = parentTerm.ContentItem;
_contentManager.Create(term, VersionOptions.Published);
}
解释:
此代码创建了一个 'Categories' 分类法。此分类法中有一个 Products
术语,这将成为所有后续子术语的父术语,因此被指定为容器。
其结果是创建了分类法,但术语不是分层的(它们都存在于一个级别)。
堆栈跟踪:
2015-02-13 15:47:32,955 [23] Orchard.ContentManagement.DefaultContentManager - (null) - NullReferenceException thrown from IContentHandler by Orchard.Autoroute.Handlers.AutoroutePartHandler
System.NullReferenceException: Object reference not set to an instance of an object.
at Orchard.Security.Authorizer.Authorize(Permission permission, IContent content, LocalizedString message) in \Site\src\Orchard\Security\Authorizer.cs:line 72
at Orchard.Security.Authorizer.Authorize(Permission permission) in \Site\src\Orchard\Security\Authorizer.cs:line 60
at Orchard.Autoroute.Handlers.AutoroutePartHandler.ProcessAlias(AutoroutePart part)
at Orchard.Autoroute.Handlers.AutoroutePartHandler.PublishAlias(AutoroutePart part)
at Orchard.Autoroute.Handlers.AutoroutePartHandler.<.ctor>b__2(PublishContentContext ctx, AutoroutePart part)
at Orchard.ContentManagement.Handlers.ContentHandler.InlineStorageFilter`1.Published(PublishContentContext context, TPart instance) in Site\src\Orchard\ContentManagement\Handlers\ContentHandler.cs:line 164
at Orchard.ContentManagement.Handlers.StorageFilterBase`1.Orchard.ContentManagement.Handlers.IContentStorageFilter.Published(PublishContentContext context) in Site\src\Orchard\ContentManagement\Handlers\StorageFilterBase.cs:line 87
at Orchard.ContentManagement.Handlers.ContentHandler.Orchard.ContentManagement.Handlers.IContentHandler.Published(PublishContentContext context) in Site\src\Orchard\ContentManagement\Handlers\ContentHandler.cs:line 288
at Orchard.ContentManagement.DefaultContentManager.<>c__DisplayClass5e.<Create>b__5b(IContentHandler handler) in \Site\src\Orchard\ContentManagement\DefaultContentManager.cs:line 534
at Orchard.InvokeExtensions.Invoke[TEvents](IEnumerable`1 events, Action`1 dispatch, ILogger logger) in Site\src\Orchard\InvokeExtensions.cs:line 17
我遇到了和你一样的问题,我在 TermAdminController 中找到了解决方案。
这是一个示例:
var parentTerm = _taxonomyService.NewTerm(categoriesTaxonomy);
parentTerm.Container = categoriesTaxonomy.ContentItem;
// Create content item before updating so attached fields save correctly
_orchardServices.ContentManager.Create(parentTerm, VersionOptions.Draft);
parentTerm.Name = "main category";
parentTerm.Selectable = true;
_taxonomyService.ProcessPath(parentTerm);
_orchardServices.ContentManager.Publish(parentTerm.ContentItem);
var subTerm = _taxonomyService.NewTerm(categoriesTaxonomy);
//here you set the parent term
subTerm.Container = parentTerm == null ? categoriesTaxonomy.ContentItem : parentTerm.ContentItem;
// Create content item before updating so attached fields save correctly
_orchardServices.ContentManager.Create(subTerm, VersionOptions.Draft);
subTerm.Name = "sub category";
subTerm.Selectable = true;
_taxonomyService.ProcessPath(subTerm);
_orchardServices.ContentManager.Publish(subTerm.ContentItem);
希望对您有所帮助!
在 Authorizer
class 的第 72 行,我收到空引用异常:
if (_authorizationService.TryCheckAccess(permission, _workContextAccessor.GetContext().CurrentUser, content))
问题是 GetContext()
返回 null
所以,很明显,引用 CurrentUser
会给我带来问题。
最初,这是一个创建分类术语的迁移,导致对授权方的调用。事实上,完整的堆栈跟踪可以在 post.
下面找到我已经尝试了重构迁移方面的所有方法,但它的代码已经在代码的其他地方被证明了 3 次(其中一次在迁移中 - 所以我确信 'context' 我这样做很好)。
迁移代码:
var taxonomy = _contentManager.New<TaxonomyPart>("Taxonomy");
taxonomy.Name = "Categories";
_contentManager.Create(taxonomy, VersionOptions.Published);
var parentTerm = _taxonomyService.NewTerm(taxonomy);
parentTerm.Name = "Products";
parentTerm.Container = taxonomy.ContentItem;
_contentManager.Create(parentTerm, VersionOptions.Published);
string[] childTermNames = { "Disks", "Mice", "Keyboards" };
foreach (var termName in childTermNames)
{
var term = _taxonomyService.NewTerm(taxonomy);
term.Name = termName;
term.Container = parentTerm.ContentItem;
_contentManager.Create(term, VersionOptions.Published);
}
解释:
此代码创建了一个 'Categories' 分类法。此分类法中有一个 Products
术语,这将成为所有后续子术语的父术语,因此被指定为容器。
其结果是创建了分类法,但术语不是分层的(它们都存在于一个级别)。
堆栈跟踪:
2015-02-13 15:47:32,955 [23] Orchard.ContentManagement.DefaultContentManager - (null) - NullReferenceException thrown from IContentHandler by Orchard.Autoroute.Handlers.AutoroutePartHandler
System.NullReferenceException: Object reference not set to an instance of an object.
at Orchard.Security.Authorizer.Authorize(Permission permission, IContent content, LocalizedString message) in \Site\src\Orchard\Security\Authorizer.cs:line 72
at Orchard.Security.Authorizer.Authorize(Permission permission) in \Site\src\Orchard\Security\Authorizer.cs:line 60
at Orchard.Autoroute.Handlers.AutoroutePartHandler.ProcessAlias(AutoroutePart part)
at Orchard.Autoroute.Handlers.AutoroutePartHandler.PublishAlias(AutoroutePart part)
at Orchard.Autoroute.Handlers.AutoroutePartHandler.<.ctor>b__2(PublishContentContext ctx, AutoroutePart part)
at Orchard.ContentManagement.Handlers.ContentHandler.InlineStorageFilter`1.Published(PublishContentContext context, TPart instance) in Site\src\Orchard\ContentManagement\Handlers\ContentHandler.cs:line 164
at Orchard.ContentManagement.Handlers.StorageFilterBase`1.Orchard.ContentManagement.Handlers.IContentStorageFilter.Published(PublishContentContext context) in Site\src\Orchard\ContentManagement\Handlers\StorageFilterBase.cs:line 87
at Orchard.ContentManagement.Handlers.ContentHandler.Orchard.ContentManagement.Handlers.IContentHandler.Published(PublishContentContext context) in Site\src\Orchard\ContentManagement\Handlers\ContentHandler.cs:line 288
at Orchard.ContentManagement.DefaultContentManager.<>c__DisplayClass5e.<Create>b__5b(IContentHandler handler) in \Site\src\Orchard\ContentManagement\DefaultContentManager.cs:line 534
at Orchard.InvokeExtensions.Invoke[TEvents](IEnumerable`1 events, Action`1 dispatch, ILogger logger) in Site\src\Orchard\InvokeExtensions.cs:line 17
我遇到了和你一样的问题,我在 TermAdminController 中找到了解决方案。
这是一个示例:
var parentTerm = _taxonomyService.NewTerm(categoriesTaxonomy);
parentTerm.Container = categoriesTaxonomy.ContentItem;
// Create content item before updating so attached fields save correctly
_orchardServices.ContentManager.Create(parentTerm, VersionOptions.Draft);
parentTerm.Name = "main category";
parentTerm.Selectable = true;
_taxonomyService.ProcessPath(parentTerm);
_orchardServices.ContentManager.Publish(parentTerm.ContentItem);
var subTerm = _taxonomyService.NewTerm(categoriesTaxonomy);
//here you set the parent term
subTerm.Container = parentTerm == null ? categoriesTaxonomy.ContentItem : parentTerm.ContentItem;
// Create content item before updating so attached fields save correctly
_orchardServices.ContentManager.Create(subTerm, VersionOptions.Draft);
subTerm.Name = "sub category";
subTerm.Selectable = true;
_taxonomyService.ProcessPath(subTerm);
_orchardServices.ContentManager.Publish(subTerm.ContentItem);
希望对您有所帮助!