我有一個.NET Core 1.1應用程序,並在HomeController的一個動作中設置了一個自定義屬性。鑑於我需要配置文件(appsettings.json)在屬性邏輯中的值,是否有可能在屬性級別訪問配置?如何在.NET Core中讀取屬性內的配置(appsettings)值?
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();
}
}
你能分享你擁有什麼,你要完成的一些代碼? – Shoe
@Shoe看到更新的問題 –
同樣的問題在這裏...你能解決它嗎? – Dzhambazov