'Settings' 由于其保护级别而无法访问
'Settings' is inaccessible due to its protection level
我的 App_Code
文件夹中有以下 Utilities.CS
文件作为整个 MVC4 应用程序使用的 "helper" 方法(构建操作设置为编译)
代码中有一个断点,如图所示...
应用程序编译 (Ctrl-Shift-B) 没有错误,但是当我 运行 应用程序时,我在断点后的后续 return
语句中得到 CS0122: 'Settings' is inaccessible due to its protection level
.
AdminGroup
设置在设置设计器中定义为 public
断点线永远不会被击中,可能是由于 运行-time 编译错误...但是如果我已经编译了它,为什么它会在 运行- 重新编译时间?
(抱歉,我是 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
class 是 internal
.
我假设您已经从 visual studio 项目属性部分设置中创建和修改了设置。 Right click on project > Properties > Settings.
有一个名为访问修饰符的下拉菜单,您需要将其从内部更改为 public。
如果您忘记在视图中引用资源文件的命名空间,即使访问修饰符更改为 public(如@Rahul Nikate 的回答所指定),也会出现此错误。
@using MyApp.Config
在 .NET Core 中,这通常位于 _ViewImports.cshtml
中,以便在相关区域中的所有视图之间共享。
我的 App_Code
文件夹中有以下 Utilities.CS
文件作为整个 MVC4 应用程序使用的 "helper" 方法(构建操作设置为编译)
代码中有一个断点,如图所示...
应用程序编译 (Ctrl-Shift-B) 没有错误,但是当我 运行 应用程序时,我在断点后的后续 return
语句中得到 CS0122: 'Settings' is inaccessible due to its protection level
.
AdminGroup
设置在设置设计器中定义为 public
断点线永远不会被击中,可能是由于 运行-time 编译错误...但是如果我已经编译了它,为什么它会在 运行- 重新编译时间?
(抱歉,我是 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
class 是 internal
.
我假设您已经从 visual studio 项目属性部分设置中创建和修改了设置。 Right click on project > Properties > Settings.
有一个名为访问修饰符的下拉菜单,您需要将其从内部更改为 public。
如果您忘记在视图中引用资源文件的命名空间,即使访问修饰符更改为 public(如@Rahul Nikate 的回答所指定),也会出现此错误。
@using MyApp.Config
在 .NET Core 中,这通常位于 _ViewImports.cshtml
中,以便在相关区域中的所有视图之间共享。