Global constant values throughout MVC site

My MVC (.NET) has some constant values through out the site, such as the phone number, physical address, email address etc.

I’d like to store this value once, so future updates are easier for when they change their details.

To make my question simpler, I’ve only focused on phone number.

My question is where and how to store this number and a problem I’m facing with my decision. At the moment, I’ve chosen to store it in the Global.asax file as a static method, eg

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
       //logic
    }

    public static string GetPhoneNumber()
    {
        return "012345 678 910";
    }
}

This works very well, in my controller I can add references to this method and assign it to a ViewBag

  public ActionResult Contact()
  {
        ViewBag.Phone = MvcApplication.GetPhoneNumber();
  }

As per many sites, the phone number will be visible at the top of every page and as such, placing it in the _Layout.cshtml is logical.

The problem I have is there is no controller (not that I know) for the _Layout.cshtml and the only way I can pass the value is to have a ViewBag.Phone set up for every view, EG

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        ViewBag.Phone = MvcApplication.GetPhoneNumber();
        return View();
    }

    public ActionResult About()
    {
        ViewBag.Phone = MvcApplication.GetPhoneNumber();
        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Phone = MvcApplication.GetPhoneNumber();
        return View();
    }

This is messy and smelly but I have no idea how I can improve this.

Should I be storing my global variables in a different place or is there a better approach for this as I’m sure this is a common problem.

Store the values in web.config:

<appSettings>
  <add key="CompanyTelNo" value="012345 678 910" />
</appSettings>

Then in your code, you can reference it with:

ConfigurationSettings.AppSettings["CompanyTelNo"];

However, since that’s actually deprecated, it’s better to use:

ConfigurationManager.AppSettings["CompanyTelNo"]

6

You can access global constants directly in your _Layout.cshtml file:

<div class="phone">@MvcApplication.GetPhoneNumber()</div>

However, I would move these constants into a separate file (I generally have a Constants.cs static class) so as not to add clutter to the global.asax file.

Also, I would have them as properties:

public static string PhoneNumber
{
    get { return "012345 678 910"; }
}

rather than methods so they could be accessed anywhere like so:

Constants.PhoneNumber

If they are likely to change often it would be better to have these values stored in a config file or database so it does not require a recompile and deployment when they need to be changed.

2

In Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
  ...
  public const string PhoneNumber = "01234 567890";
  ...
}

In _Layout.cshtml

<div>Phone number: @MvcApplication.PhoneNumber</div>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật