cshtml ファイルで Web.config の値を取得する

環境によって、cshtml のリンク先を変更したい。 下記みたいな感じ。

本番環境

<a href="https://honban.net/">リンク</a>

ステージング環境

<a href="https://staging.net/">リンク</a>

cshtml なので普通にC#のコードを記述することで取得できる。

<a href="@System.Configuration.ConfigurationManager.AppSettings["UrlKey"]">リンク</a>

Web.config の設定

<appSettings>
  <add key="UrlKey" value="https://honban.net/"/>
</appSettings>

参考

stackoverflow.com