'Settings' is inaccessible due to its protection level
Asked Answered
R

2

8

I have the following Utilities.CS file in my App_Code folder as "helper" methods used throughout my MVC4 application (Build Action set to Compile)

There is a break-point in the code as indicated...

The application compiles (Ctrl-Shift-B) with no errors but when I run the application I get a CS0122: 'Settings' is inaccessible due to its protection level at the subsequent return statement after the break-point.

The AdminGroup setting is defined as public in the Settings Designer

The break-point line never gets hit, probably due to run-time compilation error... but if I've compiled it, why is it re-compiling at run-time?

(Sorry, I'm new to MVC so no really sure what's going on)

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));
            }
        }
    }

}

UPDATE

//------------------------------------------------------------------------------
// <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"]));
            }
        }
    }
}
Renaldorenard answered 16/12, 2015 at 9:32 Comment(2)
Show code for Properties. Click on Properties and press F12Meal
Can you Properties class?Maynard
M
28

This error is because of Settings class is internal.

I assumed that you have created and modified settings from visual studio project properties section settings. Right click on project > Properties > Settings. There is a drop down menu called Access Modifier that you need to change from internal to public.

enter image description here

For more information on internal keyword: 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

Meal answered 16/12, 2015 at 10:2 Comment(1)
Odd... on one machine, my code compiles with it as "Internal". On the other it will only compile as "Public". It doesn't matter in my usage except for best practices but it's odd.Fructose
P
-2

This error can also appear if you've forgotten to reference the namespace of the Resource file in your view, even when the Access Modifier is changed to public as @Rahul Nikate's answer specifies.

@using MyApp.Config

In .NET Core, this might typically sit in _ViewImports.cshtml to be shared across all views in the associated Area.

Photoelectric answered 20/1, 2021 at 22:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.