0

In my web.config I have the following:

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

In my _Layout.cshtml I have the following:

@{
        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>
        }                               
}

When I run this I get Object reference not set to an instance of an object. What do I need to do to get this to work?

webdad3
  • 8,893
  • 30
  • 121
  • 223
  • 1
    Not sure what is wrong with this alternatively you can assign the List into `ViewBag.authList ` from server side. – Kaf Sep 26 '13 at 20:40

1 Answers1

3

My guess is you've put your setting in the wrong web.config. Double check you placed it in the appSettings section of the web.config located at project level and not the one located within the Views folder.

enter image description here

ramsey_tm
  • 792
  • 5
  • 7
  • That was it! Why do they have 2 web.config files? – webdad3 Sep 26 '13 at 21:08
  • 1
    @webdad3 The web.config in the View folder does a few things. Most significantly it blocks direct access to your views. Checkout David's answer here:http://stackoverflow.com/questions/517086/asp-net-mvc-and-two-web-config-files – ramsey_tm Sep 26 '13 at 21:13