I have a .NET Core 1.1 Application with a custom attribute set on an action in the HomeController. Given the fact that I need a value from configuration file (appsettings.json) inside the attribute logic, is it possible to access configuration at attribute level?
appsettings.json
{
"Api": {
"Url": "http://localhost/api"
}
}
HandleSomethingAttribute.cs
public class HandleSomethingAttribute : Attribute, IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
// read the api url somehow from appsettings.json
}
public void OnActionExecuted(ActionExecutedContext context)
{
}
}
HomeController.cs
public class HomeController: Controller
{
[HandleSomething]
public IActionResult Index()
{
return View();
}
}
Hi, try this in the Attribute constructor. It should be working!