Stworzyłem tę funkcję, która będzie korzystać z web.config okolicy, jeśli użytkownik korzysta z obszaru inaczej użyje web.config root:
public static T GetWebConfigSection<T>(Controller controller, string sectionName) where T : class
{
T returnValue = null;
String area = null;
var routeArea = controller.RouteData.DataTokens["area"];
if(routeArea != null)
area = routeArea.ToString();
System.Configuration.Configuration configFile = null;
if (area == null)
{
// User is not in an area so must be at the root of the site so open web.config
configFile = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
}
else
{
// User is in an Area, so open the web.config file in the Area/views folder (e.g. root level for the area)
configFile = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Areas/" + area + "/Views");
}
if (configFile != null)
returnValue = configFile.GetSection(sectionName) as T;
return returnValue;
}
a następnie zadzwonić:
ForestSettings forestSettings = ConfigFunctions.GetWebConfigSection<ForestSettings>(controller, "myCompanyConfiguration/forestSettings");
Cóż, myślę, że to nieoczekiwane i niewygodne. – Agzam
@Agzam, absolutnie się z tobą zgadzam, ale kiedy zrozumiesz, jak działa dziedziczenie web.config w ASP.NET (używając podfolderów) i gdzie Razor szuka przestrzeni nazw, zobaczysz, że ma to sens . –
Czy istnieje link do dokumentacji dotyczącej tego? – Mikhail