2015-12-16 17 views
5

Tôi có Utilities.CS tập tin sau đây trong thư mục App_Code của tôi là "helper" phương pháp sử dụng trong suốt ứng dụng MVC4 tôi (Build Action thiết để biên dịch)'Cài đặt' là không thể tiếp cận do mức độ bảo vệ của nó

Có một break-point trong mã như được chỉ định ...

Biên dịch ứng dụng (Ctrl-Shift-B) không có lỗi nhưng khi tôi chạy ứng dụng, tôi nhận được CS0122: 'Settings' is inaccessible due to its protection level tại câu lệnh sau return sau điểm ngắt.

Thiết lập AdminGroup được định nghĩa là public trong thiết kế Cài đặt

Các ngắt dòng điểm không bao giờ bị đánh, có thể là do thời gian chạy lỗi biên dịch ... nhưng nếu tôi đã biên soạn nó, tại sao nó lại tái biên dịch tại thời gian chạy?

(Xin lỗi, tôi mới để MVC vì vậy không thực sự chắc chắn những gì đang xảy ra)

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

} 

CẬP NHẬT

//------------------------------------------------------------------------------ 
// <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"])); 
      } 
     } 
    } 
} 
+0

Hiện mã cho 'Properties'. Nhấp vào 'Properties' và nhấn 'F12' –

+0

Bạn có thể lớp' Properties' không? – Kamo

Trả lời

16

Lỗi này là do Settings lớp là internal.

Tôi giả định rằng bạn đã tạo và sửa đổi cài đặt từ cài đặt thuộc tính dự án studio trực quan. Right click on project > Properties > Settings. Có một trình đơn thả xuống có tên là Trình sửa đổi truy cập mà bạn cần phải thay đổi từ bên trong thành công cộng.

enter image description here

Để biết thêm thông tin về từ khóa nội bộ: 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

+2

OMG .... Bắn tôi bây giờ .... Làm thế nào trên trái đất tôi không thấy rằng .... Có thể có được vì HDD của tôi đã chết và trên đường ra gây ra tất cả các loại vấn đề khác ,, Cảm ơn và xin lỗi vì lãng phí thời gian của bạn –

+0

chào mừng @ChrisHammond –

+3

Cảm ơn rất nhiều! Tôi đã dành khoảng một giờ cố gắng tìm ra những gì tôi đang làm sai. – SwiftHanded

Các vấn đề liên quan