2017-09-14 6 views
0

web.configファイルのさまざまなセクションの値を読みたい。web.configファイルの値を読み取る

私はのhttpRuntime requestValidationMode 値を読みたいこのsystem.webセクションで

<system.web> 
<compilation debug="true" targetFramework="4.5.2" /> 
<httpRuntime targetFramework="4.5.2" requestValidationMode="4.5" 
maxRequestLength="102400" /> 
<pages validateRequest="true"></pages> 
</system.web> 

。ページでは、validateRequestの値を読み取る必要があります。

はまた、私はあなたが他の値については、この

ConfigurationSection httpProtocolSection = 
    ConfigurationManager.GetSection("system.webServer/httpProtocol"); 
ConfigurationElementCollection customHeadersCollection = httpProtocolSection.GetCollection("customHeaders");  

ような何かをしたいと考えているカスタムヘッダーの値

<httpProtocol> 
    <customHeaders> 
    <add name="X-Content-Type-Options" value="nosniff" /> 
    <add name="X-Frame-Options" value="SAMEORIGIN" /> 
    <remove name="X-Powered-By" /> 
    <add name="X-XSS-Protection" value="1; mode=block" /> 
    </customHeaders> 
</httpProtocol> 

答えて

0

を読みたい、あなたはその値が格納されている場所を検索する必要があります。例えば

<compilation debug="true" ...> 

がここに格納され

HttpContext.Current.IsDebuggingEnabled 

は、セクションタイプの多くは、さらに、System.Web.Configuration

System.Web.Configuration.PagesSection 
System.Web.Configuration.HttpHandlersSection 

に格納されているそして、あなたは(GetSectionを有するものを取得することができます) 同じように。

+0

NameValueCollectionのようなVARのcustomHeaders =(AppSettingsSection)ConfigurationManager.GetSection( "customHeaders")と VaRのcustomHeaders = ConfigurationManager.GetSection( "customHeaders")の両方がヌル値を戻すしています。 – ABB

+0

私はそれを編集しました。私はそれのためのセクションのXPathが必要であることを理解しました。 –

+0

私はこのコードをglobal.asax.csに書いています。私はweb.configファイルを読んでそれを検証したいです – ABB

関連する問題