How do I get a const string from Web.config?

asp.net-mvc authorization c# const entity-framework

Accepted Answer

What if the AppSettings key was kept as a constant and its value was retrieved fromIsAuthorized method:

public class AuthorizeDesignatedRoles : AuthorizeAttribute
{
    public const string DELETE = "GroupAuthorizedForDeleteAction";

    public string DesignatedRoles { get; set; }

    protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext) {
    {
        // ...

        string[] roles = DesignatedRoles.Split(';')
                                        .Select(s => ConfigurationManager.AppSettings[s].ToString())
                                        .ToArray();

        foreach (string role in roles)
        {
            // ...
        }
    }
}
2
3/10/2014 10:37:00 AM

Popular Answer

Summary: Usereadonly instead ofconst .

Constants must be set at the time of compilation and cannot change in updates that are compatible. Constant values from referenced assemblies can and are copied by the compiler into the output assembly.

Code can be written to set read-only values before type references (static readonly (member) or the constructorreadonly ) but no more changes are then implemented.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow