0

我在 web.config 中声明我的价值观

 <appSettings>
    <add key="SystemName" value="RealState Premium" />
    <add key="SystemDescription" value="Sistema de Administração Imobiliário" />
  </appSettings>

配置模型:

    public class _Header
    {
        public string SystemName { get; set; }
        public string SystemDescription { get; set; }
    }

控制器:

    [AllowAnonymous]
    public ActionResult _Header()
    {
        HomeModels._Header headerModel = new HomeModels._Header();

        headerModel.SystemName = ConfigurationManager.AppSettings["SystemName"];
        headerModel.SystemDescription = ConfigurationManager.AppSettings["SystemDescription"];

        return PartialView(headerModel);
    }

最后,查看:

@using realstate.Models
@model HomeModels._Header

<div class="logo">
    <img src="~/Images/logo/logo.png" alt="Mercado de Imóveis" />
    <table class="sysTitleTbl">
        <tr>
            <td class="name">@Model.SystemName</td>
        </tr>
        <tr>
            <td class="description">@Model.SystemDescription</td>
        </tr>
    </table>
</div>

问题是我在@Model.SystemName 中得到了一个空引用,这会是什么问题?

4

2 回答 2

1

为这些代码行设置断点

headerModel.SystemName = ConfigurationManager.AppSettings["SystemName"];
headerModel.SystemDescription = ConfigurationManager.AppSettings["SystemDescription"];

看看发生了什么

于 2013-05-24T06:24:25.233 回答
0

确保在“/web.config”文件而不是“views/web.config”文件中设置 appSettings 节点。另外,请确保它位于“配置”中。

例子:

<configuration>
    <appSettings>
        <add key="SystemName" value="RealState Premium" />
        <add key="SystemDescription" value="Sistema de Administração Imobiliário" />
    </appSettings>
</configuration>
于 2013-05-23T21:29:53.527 回答