webpages:Enabled AppSetting in MVC

If you open your web.config at root of your MVC project you will find bunch of default appsetting keys added by MVC or WebAPI.

One of the appsetting with key webpapges:Enabled is added which as default value as false.

So what is the deal with this appsetting?

To find out the answer to this simple question I started searching ASP.NET WebStack codebase on codeplex.

This appsetting is processed by the System.Web.WebPages.Deployment - WebPagesDeployment.cs code.

Dry-running the code base I got answer to my query.

Three possibility for the appsetting - webpages:Enabled:

  1. Key - value not added at all or not having a valid boolean value.
  2. Key - value added and set to false.
  3. Key - value added and set to true.

Case 1: In this case when PreApplicationStartCode > Start method is called which in turn calls > StartCore which internaly checks this application setting. If the value is not found or is null. The code base will search for any *.cshtml or *.vbhtml extension files and if found Any() then returns true or else false.

Case 2: In this case access to all view files directly is not allowed and you get error - This type of page is not served. Check by creating a Test.cshtml file in project root and try accessing directly via URL request, you will received a error similar to this one:

View access forbidden

Case 3: In this case access to any view files with *.cshtml or *.vbhtml extension will be served without any restrictions.

Now it gets interesting

Reference to my previous post of this Mordern ASP.NET (man series) # 002 we talked about how access to view files directly under Views folder is restricted and redirected to HttpNotFoundHandler.

If you carefully observe the web.config files under the Views folder then you will observed that it also contains the webpages:Enabled application setting.

After playing a while with this setting and other settings in this web.config, I understand that this application setting plays important part in restricting access to *.cshtml or *.vbhtml from Views folder.

If this application setting ALONG with the config section from previous blog post (link mentioned above) are not set (either / or) the you will encounter following error:

View does not inherit from System.Web.WebPages.WebPage

Type 'ASP._Page_Views_cshtml' does not inherit from 'System.Web.WebPages.WebPage'.

Moral: This tini-tiny application setting is important.

Happy Coding !!