2010-06-11 15 views
13

से system.web खंड को पढ़ने के लिए आसान होना चाहिए, लेकिन जो कुछ भी मैं कोशिश रिटर्न शून्य:कैसे web.config

const string key = "system.web"; 

var sectionTry1 = WebConfigurationManager.GetSection(key); 

var sectionTry2 = ConfigurationManager.GetSection(key); 

मुझे यकीन है कि मैंने यह किया है से पहले कर रहा हूँ।

यदि मैं इससे कोई फर्क पड़ता हूं तो मैं एमवीसी का उपयोग कर रहा हूं।

उत्तर

23

बेवकूफ था - system.web कॉन्फ़िगरेशन अनुभाग नहीं है लेकिन एक कॉन्फ़िगरेशन समूह है। यदि मैं एक वास्तविक खंड में कुंजी बदलता हूं, तो दोनों विधियां ठीक काम करती हैं।

const string outputCacheSettingsKey = "system.web/caching/outputCacheSettings";   

var outputCacheSettingsSection = ConfigurationManager.GetSection(outputCacheSettingsKey) as OutputCacheSettingsSection; 
+0

रिकॉर्ड के लिए वही काम करता है यदि आपको संकलन अनुभाग प्राप्त करने की आवश्यकता है, सिवाय इसके कि आप इसे 'संकलन जांच' –

5

मुझे लगता है कि system.web एक्सेस करना ऐप सेटिंग्स एक्सेस करने के लिए थोड़ा अलग है।

इस प्रयास करें:

string configPath = "/MyAppRoot"; 

Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath); 

IdentitySection section = (IdentitySection)config.GetSection("system.web/identity"); 

आप system.web के प्रासंगिक अनुभाग आप एक विशेष प्रकार का उपयोग करने की कोशिश कर रहे हैं कास्ट करने के लिए की जरूरत है।

+0

पहले, जांच करता है, तो 'system.web/identity' *** अनुभाग मौजूद है ***? – Kiquenet

4

यह मेरे लिए काम किया:: यहाँ ConfigurationManager का उपयोग कर एक है

public Int32 GetmaxRequestLength() 
{ 
    // Set the maximum file size for uploads in bytes. 
    var section = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection; 
    // return length converted to kbytes or return default value as specified 
    return (Int32) Math.Round((decimal)(section != null ? (double)section.MaxRequestLength * 1024/1000 : 5.120), 2); 
} 
+0

'कॉन्फ़िगरेशन प्रबंधक.गेटसेक्शन' में डालते हैं *** machine.config *** या *** web.config ** *? केवल मैं *** web.config *** पढ़ना चाहता हूँ – Kiquenet