c# webconfig的自定義的使用方法

首先在web.config中的 configSections裏添加定義你的section

<configSections>
     <section name="picLink" type="System.Configuration.NameValueSectionHandler"/>
</configSections>

然後在configuration中添加你的namevalue項

 <picLink>
    <add key="link" value="http://www.qnwcn.com/"></add>
  </picLink>

最後的調用方法爲

using System.Configuration;
using System.Collections.Specialized;
namespace PPP.Code
{
    public static class picLinkClass
    {

        public static string  getPiclink(){
            NameValueCollection postinfo = (NameValueCollection)ConfigurationManager.GetSection("picLink");
            return postinfo["link"];
        }

    }
}

在下一步你就可以在cshtml或cs裏使用了

@using PPP.Code
<ul>
    <li>@picLinkClass.getPiclink()</li>
</ul>
發佈了22 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章