繁体   English   中英

OnActionExecuted方法无法在.NetCore 2.0中重写

[英]OnActionExecuted method cant be overriden in .NetCore 2.0

在我过去用过测试各种项目的测试应用程序中,我有一个基本控制器。

BaseController(MVC5)

    public abstract class BaseController : Controller
    {
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (User != null)
            {
                var context = new ApplicationDbContext();
                var username = User.Identity.Name;
                var iconSource = ServicesAppSettings.UserIcons.FemaleUserIconSource;

                if (!string.IsNullOrEmpty(username))
                {
                    var user = context.Users.SingleOrDefault(u => u.UserName == username);
                    if (user != null)
                    {
                        var gender = user.GetUserGender(username);
                        if (gender == DomainClasses.Enums.Gender.Male)
                        {
                            iconSource = ServicesAppSettings.UserIcons.MaleUserIconSource;
                        }
                        var fullName = user.GetFullName(username);
                        ViewData.Add("FullName", fullName);
                        ViewData.Add("IconSource", iconSource);
                    }
                }
            }
            base.OnActionExecuted(filterContext);
        }       
    }

我现在正在为自己构建一个Core 2.0 Web模板,并且正在尝试在Core 2.0中实现基本控制器,但该方法错误。

BaseController(MVC 6,.Net core 2.0)

protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (User != null)
        {
            var baseArgs = new[] { "TestApp" };
            var context = ApplicationDbContextFactory.CreateDbContext(baseArgs);
            var username = User.Identity.Name;
            var iconSource = _config.GetSection("FemaleUserIcon");

            if (!string.IsNullOrEmpty(username))
            {
                var user = context.Users.SingleOrDefault(u => u.UserName == username);
                if (user != null)
                {
                    var gender = user.GetUserGender(username);
                    if (gender == Gender.Male)
                    {
                        iconSource = _config.GetSection("MaleUserIcon");
                    }
                    var fullName = user.GetFullName(username);
                    ViewData.Add("FullName", fullName);
                    ViewData.Add("IconSource", iconSource);
                }
            }
        }
        base.OnActionExecuted(filterContext);
    }

并且错误消息与受保护的方法有关

'BaseController.OnActionExecuted(ActionExecutedContext)':覆盖'public'继承成员时无法更改访问修饰符

这在MVC 5中运行得非常好,现在使用MVC 6和.Net Core 2,这是失败的。 知道为什么在这种情况下这种方法不能被保护或私有吗? 我不明白为什么需要公开。

此外,现在我正在重新查看此代码,这是每次调用控制器操作时检索用户,不是吗? 在用户第一次登录时,必须有更好的方法来存储此信息.SqlDistributedCache?

基本Controller类中方法的访问修饰符是public而不是protected

public virtual void OnActionExecuted(ActionExecutedContext context);

您也可以在官方源代码中看到这一点

编译器错误告诉您无法在继承的类中更改它。 所以你只需要改变你的匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM