c# - “设置”由于其保护级别而无法访问

标签 c# asp.net-mvc class asp.net-mvc-4 access-modifiers

我的 App_Code 文件夹中有以下 Utilities.CS 文件作为整个 MVC4 应用程序中使用的“帮助程序”方法(Build Action 设置为 Compile)

代码中有一个断点,如图所示...

应用程序编译 (Ctrl-Shift-B) 没有错误,但是当我运行应用程序时,我得到一个 CS0122: 'Settings' is inaccessible due to its protection level 在随后的 断点后返回语句。

AdminGroup 设置在设置设计器中定义为 public

断点线永远不会被击中,可能是由于运行时编译错误...但是如果我编译了它,为什么它在运行时重新编译?

(抱歉,我是 MVC 的新手,所以不太确定发生了什么)

namespace MyApplication
{
    public class Utilities
    {
        public static string UserID
        {
            get
            {
                return Regex.Replace(WindowsIdentity.GetCurrent().Name, @".+\\", "").ToUpper();
            }
        }

        public static bool IsAdmin
        {
            get
            {
                System.Diagnostics.Debug.WriteLine("Break point on this line");
                return (HttpContext.Current.User.IsInRole(Properties.Settings.Default.AdminGroup));
            }
        }
    }

}

更新

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MyApplication.Properties {


    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

    //
    // Other Settings Removed
    //

        [global::System.Configuration.ApplicationScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("MYDOMAIN\\ADMINGROUP")]
        public string AdminGroup {
            get {
                return ((string)(this["AdminGroup"]));
            }
        }
    }
}

最佳答案

这个错误是因为 Settings 类是 internal

我假设您已经从 visual studio 项目属性部分设置中创建和修改了设置。 右键单击项目 > Properties > Settings。有一个名为 Access Modifier 的下拉菜单,您需要将其从 internal 更改为 public。

enter image description here

有关内部关键字的更多信息: The internal keyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly

关于c# - “设置”由于其保护级别而无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34308345/

相关文章:

javascript - 网页无法正确加载: '"JSON"is undefined' error

c# - SignalR 从 Controller 加入组

class - 从另一个 *.ts 文件创建 typescript 的对象

java - 如何循环字符串数组?类(class)?

c# - 如何对 DataView 中的日期时间列进行排序?

c# - 在服务器上发布我的 Core 2.2 项目后 web.config 无效

c# - 当数据为 null 或为空时,在 @Html.DisplayFor 中插入不间断空格? ASP.Net MVC

asp.net-mvc - ASP.NET MVC - 如何从不同的 Controller 维护 ModelState?

c# - ASP.Net MVC - 集合未在回发时填充的模型

java - 如何访问我只通过字符串名称知道的类的类字段?