C# 在电子商务中获得客户角色

C# 在电子商务中获得客户角色,c#,razor,nopcommerce,C#,Razor,Nopcommerce,有人能帮我在2.8版的视图中获得客户角色吗?我尝试了workcontext,错误是“workcontext在当前上下文中不存在”。我有下面的代码,但它不起作用 @model BoardsIndexModel @using Nop.Core.Infrastructure; @using Nop.Services.Helpers; @using Nop.Web.Models.Boards; @using Nop.Core.Domain.Customers; @using Nop.Services.C

有人能帮我在2.8版的视图中获得客户角色吗?我尝试了workcontext,错误是“workcontext在当前上下文中不存在”。我有下面的代码,但它不起作用

@model BoardsIndexModel
@using Nop.Core.Infrastructure;
@using Nop.Services.Helpers;
@using Nop.Web.Models.Boards;
@using Nop.Core.Domain.Customers;
@using Nop.Services.Customers;


@if (Roles.IsUserInRole("Administrators"))
{
    Do that;
}
else
{
    Do this;
}
你可以用

@User.IsInRole("Administrators")
你可以用

@User.IsInRole("Administrators")

我就是这样做的,在nopcommerce论坛上的一位好人的帮助下:

@using Nop.Core.Domain.Customers;
@using Nop.Services.Customers;
@using Nop.Core;
@{
    bool customerHasRoleX = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("Administrators");
    bool customerHasRoleY = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("ForumModerators");
}

@if ((customerHasRoleX == true) | (customerHasRoleY == true)) {

}
else
{
    Response.Redirect("~/login?ReturnUrl=%2fboards");
}
@使用Nop.Core.Domain.Customers;
@使用Nop.Services.Customers;
@使用Nop.Core;
@{
bool customerhastrolex=EngineContext.Current.Resolve().CurrentCustomer.IsInCustomerRole(“管理员”);
bool customerHasRoleY=EngineContext.Current.Resolve().CurrentCustomer.IsInCustomerRole(“ForumModelators”);
}
@如果((customerhastrolex==true)|(customerhastroley==true)){
}
其他的
{
重定向(“~/login?ReturnUrl=%2fboards”);
}

在nopcommerce论坛上的一位好心人的帮助下,我就是这样做的:

@using Nop.Core.Domain.Customers;
@using Nop.Services.Customers;
@using Nop.Core;
@{
    bool customerHasRoleX = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("Administrators");
    bool customerHasRoleY = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("ForumModerators");
}

@if ((customerHasRoleX == true) | (customerHasRoleY == true)) {

}
else
{
    Response.Redirect("~/login?ReturnUrl=%2fboards");
}
@使用Nop.Core.Domain.Customers;
@使用Nop.Services.Customers;
@使用Nop.Core;
@{
bool customerhastrolex=EngineContext.Current.Resolve().CurrentCustomer.IsInCustomerRole(“管理员”);
bool customerHasRoleY=EngineContext.Current.Resolve().CurrentCustomer.IsInCustomerRole(“ForumModelators”);
}
@如果((customerhastrolex==true)|(customerhastroley==true)){
}
其他的
{
重定向(“~/login?ReturnUrl=%2fboards”);
}

这也不起作用。它的行为方式是用户不在角色中,但我知道用户在角色“Administrators”中。我尝试了…@if(user.IsInRole(“Administrators”){Do that;}else{Do this;}这看起来是一个简单的解决方案,但不幸的是,它对我也不起作用。这也不起作用。它的行为方式是用户不在角色中,但我知道用户在角色“Administrators”中。我尝试了…@if(user.IsInRole(“Administrators”){Do that;}else{Do this;}这看起来是一个简单的解决方案,但不幸的是,它对我也不起作用。