开发者

Where to store defaults asp.net

I have bunch of default strings and ints throughout the app. like default names, etc,etc. Those are all constants. I just want to 开发者_StackOverflowaccess them in different pages in my app. What is the best place to place them? I was thinking about Enum or something similar. What is the best approach?


The best place is to put them into the appSettings section of your web.config file:

  <appSettings>
    <add key="myKey" value="myValue"/>
  </appSettings>

You can then read the key/value pairs using the ConfigurationManager class:

' Get the AppSettings section.        
' This function uses the AppSettings property
' to read the appSettings configuration 
' section.
Public Shared Sub ReadAppSettings()
    Try
        ' Get the AppSettings section.
        Dim appSettings As NameValueCollection = _
            ConfigurationManager.AppSettings

        ' Get the AppSettings section elements.
        Console.WriteLine()
        Console.WriteLine("Using AppSettings property.")
        Console.WriteLine("Application settings:")

        If appSettings.Count = 0 Then
            Console.WriteLine( _
            "[ReadAppSettings: {0}]", _
            "AppSettings is empty Use GetSection first.")
        End If
        Dim i As Integer = 0
        While i < appSettings.Count
            Console.WriteLine( _
                "#{0} Key: {1} Value: {2}", _
                i, appSettings.GetKey(i), appSettings(i))
            System.Math.Max( _
                System.Threading.Interlocked. _
                Increment(i), i - 1)
        End While
    Catch e As ConfigurationErrorsException
        Console.WriteLine("[ReadAppSettings: {0}]", _
                          e.ToString())
    End Try
End Sub


You could simply create a static class with each public const.

static public class DefaultValues
{
    public const string Name = "Bob";
    public const int AppId = 1234;
}


The best approach is to have some kind of definitions file. However, be careful with approach, as you can easily get an unmanageable definitions file if you keep adding random constants to it.


I generally create a constants class or some other appropriately named static class and then put static get only properties for the various constants.

public static string ConstantString { get {return "Hello World"; } }


I keep things like this in the project's Settings area (Project Properties -> Settings) and then access them throughout my code as My.Settings.<TheSettingName> in a strongly-typed manor.

Also as a bonus, it puts each of these settings then in the web.config file, where I could then change their values at runtime if need-be, without the need to recompile/deploy.

The downside of this is that My.Settings.<...> isn't available right from the view/aspx. So for values that I need right in the view, I wrap them in an accessor module. E.g.

Public Module ScriptReferences
    Public Function JQueryJsUrl() As String
        Return My.Settings.JQueryJsUrl
    End Function
End Module

and then in the view/aspx you can simply call ScriptReferences.JQueryJsUrl to get your value.


You could also use the App_GlobalRessource with resource file ... That way those constant can be easily maintained.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜