簡體   English   中英

Web.Config自定義部分返回null

[英]Web.Config custom section returns null

好吧,我的Web配置中有以下條目用於ASP.net MVC項目中的SagePay集成。

<configuration>
  <configSections>
<section name="SagePayConfiguration" type="System.Configuration.IgnoreSectionHandler" requirePermission="true" restartOnExternalChanges="true" allowLocation="true"/>

</configSections> 

<SagePayConfiguration>
    <!--Mandatory     
    Set to TEST for the Test Server and LIVE for the live environment-->
    <add key="sagepay.api.env" value="TEST" />
    .
    .
</SagePayConfiguration>

但是,嘗試從C#代碼訪問它時,我得到的是null。

SagePayConfiguration sagePayConfiguration = (SagePayConfiguration)System.Configuration.ConfigurationManager.GetSection("SagePayConfiguration");

有什么幫助我在這里想念的嗎?

您必須將ConfigrationItem強制轉換為“ System.Configuration.IgnoreSectionHandler”,或將web.conf中的Attribute類型設置為程序集中的現有Type。

<section name="<SectionName>" type="<your type (FQN)>, <Assembly>" />

之后,您可以訪問它

YourType o =(((YourType)ConfigurationManager.GetSection(“ sectionName”)));

編輯:

類型必須派生自ConfigurationSection。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM