11

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.

12

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
  • How would I access this value from the _Layout page?
    – Dave
    Dec 31 '13 at 11:22
  • Similar to what @Richard-dalton said above. except not having the actual values in a class. If those values change, you need to recompile. If they are in config, then you just change them in IIS.
    – Lotok
    Dec 31 '13 at 11:25
  • 1
    I think the ConfigurationSettings is obsolete, I used ConfigurationManager.AppSettings["MyKey"] in the exact same manner.
    – Dave
    Jan 8 '14 at 15:59
  • 1
    Note: I had to use @System.Configuration.ConfigurationManager.AppSettings["myKey"] Jan 30 '14 at 17:06
  • 1
    Same as @DaveRook, except you don't have a Using System.Configuration; declaration at the top of your file
    – Lotok
    Jan 30 '14 at 21:25
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
  • 2
    He could also use a simple .resx-File :)
    – Knerd
    Apr 7 '14 at 8:53
  • @MvcApplication is not recognized in view files ...
    – thedrs
    Jan 20 '15 at 13:57
1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.