'How can I access web.config values from within a class attribute?
I have the following EnableCors attribute on a WebApi controller class:
[EnableCors(origins:"http://localhost:57280", headers: "*", methods: "*")]
public class PhysicalActivityController : ApiController
But I don't want that url hard-coded. I want to fetch it from web.config.
Something like this:
[EnableCors(origins: System.Web.Configuration.WebConfigurationManager.AppSettings["ModellingApiUrl"], headers: "*", methods: "*")]
public class PhysicalActivityController2 : ApiController
The problem is that I can't access methods like that from within an attribute. The context of the code is not the same as being within an ordinary method. When I write the code as in the second example, Visual Studio highlights "AppSettings" in red, and gives the error "Cannot resolve symbol 'AppSettings'".
Any ideas on how I can achieve this? I tried writing a custom attribute that inherits from EnableCors, but sadly System.Web.Http.Cors.EnableCorsAttribute is a sealed class, so I can't inherit from it.
Solution 1:[1]
I solved this in the end by a wholesale copy of the code for the EnableCors attribute, and then I modified it to fetch values from the config.
The problem is that the string which is passed into the attribute is an argument to its constructor, and needs to be a compile-time constant. Therefore I don't believe that what I wanted to do was possible - you have to edit the code behind the attribute.
My solution:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
[SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments", Justification = "Attribute arguments are accessible as collections.")]
public sealed class EnableCorsUsingConfigAttribute : Attribute, ICorsPolicyProvider
{
public EnableCorsUsingConfigAttribute(string configKeyForOrigins, string headers, string methods)
: this(configKeyForOrigins, headers, methods, null)
{
}
public EnableCorsUsingConfigAttribute(string configKeyForOrigins, string headers, string methods, string exposedHeaders)
{
string origins = System.Web.Configuration.WebConfigurationManager.AppSettings[configKeyForOrigins];
(the rest of the code is a direct copy of the EnableCors attribute)
Usage:
[EnableCorsUsingConfig(configKeyForOrigins: "ModellingApiClientUrls", headers: "*", methods: "*")]
public class PhysicalActivityController : ApiController
{
Solution 2:[2]
System.Configuration.ConfigurationManager.ConnectionStrings["ModellingApiUrl"].ToString();
use this code i hope this is useful to you
Solution 3:[3]
you just need to add :
ConfigurationManager.AppSettings["AllowedOriginsCors"].ToString();
the external file :
<appSettings>
<add key="AllowedOriginsCors" value="http://localhost:4200" />
</appSettings>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ClareSudbery |
Solution 2 | 123456 |
Solution 3 | CHAHI Saad |