Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does mvc support inheritance of Web.config settings throughout areas?

I distributed my MVC code into a few different areas and noticed one thing. if I have something in the main Web.config, something like:

  <system.web.webPages.razor>
     <pages pageBaseType="System.Web.Mvc.WebViewPage">
       <namespaces>
         <add namespace="System.Collections.Generic" />

those pages that don't belong to the root area know nothing about that. And I have to repeat the same thing in the inner Web.config, the one that sits in the area folder.

How come?

like image 764
iLemming Avatar asked May 09 '11 20:05

iLemming


People also ask

What is the use of Web config in MVC?

A configuration file (web. config) is used to manage various settings that define a website. The settings are stored in XML files that are separate from your application code. In this way you can configure settings independently from your code.

Why are there two web config files in MVC?

This structure allows you to achieve the level of configuration detail that your applications require at the appropriate directory levels without affecting configuration settings at higher directory levels. Common configurations in the /Views/web.

How many configuration files are there in MVC?

You can have 1 Web. config file per folder . So in your root you can have a web. config and in a subfolder you can have another one that overrides settings from the one in the root.

Does ASP support inheritance?

TPH is the only inheritance pattern that the Entity Framework Core supports. What you'll do is create a Person class, change the Instructor and Student classes to derive from Person , add the new class to the DbContext , and create a migration.


2 Answers

web.config inherit but only to subfolders. ~/Areas is a separate folder from ~/Views so what you put in ~/Areas/SomeAreaName/Views/web.config has nothing in common with what you put in ~/Views/web.config. And because Razor ignores the namespaces section in ~/web.config you kinda need to repeat it for areas.

In summary you have:

  • ~/Views/web.config
  • ~/Areas/SomeAreaName/Views/web.config

which are two completely distinct folders and sections in them cannot be inherited.

like image 197
Darin Dimitrov Avatar answered Jun 16 '23 13:06

Darin Dimitrov


I created a function to do this which will use the area web.config if the user is using the area otherwise will use the root web.config:

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;
        }

And then call:

ForestSettings forestSettings = ConfigFunctions.GetWebConfigSection<ForestSettings>(controller, "myCompanyConfiguration/forestSettings");
like image 33
Jon Avatar answered Jun 16 '23 13:06

Jon