2013-09-26 14 views
0

在我的web.config我有以下幾點:試圖從web.config中讀取 - 收到錯誤

<appSettings> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="authorizedUsers" value="jeff,jason,bob"/> 
    </appSettings> 

在我_Layout.cshtml我有以下幾點:

@{ 
     List<String> authList = new List<string>(); 
     authList = System.Web.Configuration.WebConfigurationManager.AppSettings["authorizedUsers"].Split(',').ToList(); 

     if (authList.Any(u=>u == this.User.Identity.Name)) 
     { 
      <li>@Html.ActionLink("Admin", "Index", "Admin")</li> 
     }        
} 

當我運行這我得到Object reference not set to an instance of an object.我需要做些什麼才能使這個工作?

+1

不知道什麼是不對的或者你可以將列表從服務器端分配到''ViewBag.authList。 – Kaf

回答

3

我的猜測是你把你的設置放在了錯誤的web.config中。仔細檢查一下,是否將它放置在位於項目級別的web.config的appSettings部分中,而不是位於Views文件夾內的那個部分。

enter image description here

+0

就是這樣!爲什麼他們有2個web.config文件? – webdad3

+1

@ webdad3 View文件夾中的web.config會執行一些操作。最顯着的是它阻止了直接訪問您的意見。結帳大衛的答案在這裏:http://stackoverflow.com/questions/517086/asp-net-mvc-and-two-web-config-files –