214

I just started a new MVC 3 project. Can anyone tell me what

<add key="webpages:Enabled" value="false" />

in my web.config file does?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
ftnilsson
  • 2,193
  • 2
  • 12
  • 10
  • 4
    http://blogs.msdn.com/b/marcinon/archive/2011/01/13/mvc-3-project-upgrade-tool.aspx – Lazarus Feb 03 '11 at 12:26
  • 1
    It would be helpful to note what happens if this is set and you attempt to browse a .chstml file. You get the error "This type of page is not served." Spent hours on this one :/ http://stackoverflow.com/questions/12533605/iis-express-webmatrix-2-wont-serve-cshtml-files-in-webapi-project-vs10-winx/12535137#12535137 – Dave Swersky Sep 23 '12 at 15:14

3 Answers3

254

webPages:enabled with value false prevents .cshtml or .vbhtml files in the Views folder from being directly accessible from a web browser.

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
  • 21
    Actually it prevents them from being access directly regardless of where they reside in the application. In the views folder, in the root, in a different folder... doesn't matter you get the same error. `The type of page you have requested is not served because it has been explicitly forbidden` – Nick Albrecht Jan 31 '13 at 19:03
  • 5
    Isn't that what the "BlockViewHandler" configuration is for? What is the difference? – Tom Pažourek Nov 20 '14 at 22:16
  • Sorry, running a Nancy project on Mono and this setting appeared via nuget. So I wonder, is this an IIS thing, and MVC 3 thing, or which part of the stack actually consumes this setting? – Frank Schwieterman May 27 '15 at 00:39
  • I'm not allowed to see Views by default, but if i add this setting and set it to false, they are now parsed and attempted to be viewed. – Brunis Jan 04 '19 at 14:34
  • WebPages:Enabled enables it whether the value is true or false, the default value is null. – Brunis Jan 04 '19 at 14:37
29

According to documentation it prevents .cshtml and .vbhtml files from being accessed directly (www.myweb.com/views/home/index.cshtml for example)

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
Lukáš Novotný
  • 9,012
  • 3
  • 38
  • 46
  • 1
    I've been searching, but can't find a reference where this is explained. What I'd specifically like to know is what is the effect of omitting `` entirely. Is the default `true` or `false`? – StarNamer Mar 11 '14 at 11:04
  • 3
    So I've dug into the source code and the default value is actually `null` - meaning that webpages are enabled if application contains any `*.cshtml` or `*.vbhtml` files. Checking for files can be either disabled by setting this value to `true` or `false`. You can see for yourself [here](https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.WebPages.Deployment/WebPagesDeployment.cs). – Lukáš Novotný Mar 13 '14 at 21:33
  • According to which documentation? – Kissaki May 12 '20 at 09:26
1

To allow Razor pages to be served, it's not enough to set <add key="webpages:Enabled" value="true" />. You also need to add the Microsoft.AspNet.WebPages package to packages.config using NuGet.

Ron Inbar
  • 2,044
  • 1
  • 16
  • 26