I was exploring the source of the default MVC 5 projects and found that there are two Web.Config files created. One is in the root of the project and the other in the root of Views. Why are there two?
The web.config file exists in the Views folders to prevent access to
your views by any means other than your controller. In the MVC design
pattern, controllers are supposed to route requests and return a
rendered view to the calling client. In other words, your view at
www.mydomain.com/MySuperController/AwesomeAction1/SweetPage.aspx
should not be directly accessible.
What does the Web.Config file do in the views folder of a MVC project
All configuration files in IIS are hierarchical. You can have one in every directory, if you want to, and each lower-level configuration overrides the higher level ones.
In the machine-level configuration there are definitions for what each of the sections mean and which web.config files those sections can appear in, which builds up a pretty complex system of settings that can be changed at each level. See, for example, this article on Working with Configuration Files in IIS 7, in particular the section Configuration Levels
In the case of an MVC application, the top-level configuration file defines the web server and web application settings that apply to your entire project. The web.config file that's in your Views folder overrides those settings, where needed, and adds additional settings that only apply to the actual Razor views in your project, and not (say) the App_Data folder or your Global.asax. For example, the web.config for your views adds an additional assembly reference that adds an XML tag for the MVC namespace, which only makes sense in the context of an HTML page:
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
Related
There is one in the Views folder.
And there is another in the root of the app.
I want to register a custom handler and i cant understand where the code should go.
I'm running IIS7 in Integrated Mode so I have to add a <handlers> tag to the <system.webServer> but when I'm looking at the web.config at the Views folder I see that it uses a <httpHandlers> under <system.web> tag.
So two questions:
1. why are there two web.config files in an mvc application?
2. where and in what way I should register my custom HTTP Handler?
You should register it in root config. Views config is for configure views, for example: add namespace for all views and etc.
ASP.NET MVC and two Web.config files
why are there 2 web.config files
In Asp.net MVC Razor we can use the _ViewStart.cshtml file to define the default layout for our views. That is fine, but why did we loose the web.config setting that was available with web forms engine in system.web > pages.masterPageFile setting?
Why is that important? Because web.config allow automatic environment transformations.
Manual solution
Of course we can put the default layout name in either appSettings and refer to that in the _ViewStart.cshtml or if we have application specific configuration section add additional property which makes things a bit more clear and polished...
But is there any other way that doesn't include code?
Asp.net MVC team should add the layoutFile configuration setting to system.web.webPages.razor > pages element, shouldn't they?
The main question is
Am I missing something that is built-in and I don't know about? Something that requires no code at all similar to WebForms engine?
Asp.net MVC team should add the layoutFile configuration setting to
system.web.webPages.razor > pages element, shouldn't they?
Feel free to open a ticket on the MS Connect site.
But while waiting for your ticket to be analyzed by the development teams and probably added in a future version of the ASP.NET MVC framework and the WebPages you could always do the following in your _ViewStart.cshtml:
#{
Layout = ConfigurationManager.AppSettings["Layout"];
}
and then define the layout in your web.config file:
<appSettings>
<add key="Layout" value="~/Views/Shared/_Layout.cshtml" />
...
</appSettings>
Another possibility is to write a custom razor view engine in which you set the layout dynamically.
I have a solution divided in 3 projects:
Domain
Web User Interface
Unit tests
In my web.config, I defined my database like this:
<connectionStrings>
<add name="EntityFrameworkDbContext"
connectionString="Data Source=.\SQL2008;Initial Catalog=MvcLearning;Integrated Security=SSPI;Database=MVCLearning;AttachDBFilename=|DataDirectory|MVCLearning.mdf;MultipleActiveResultSets=True;User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
We have the |DataDirectory| used in my connectionstring.
Everything about the database should be located in the 'domain' projet, right? But when executing the solution for the first time, the database is created and I see that it is created in the WebUI\App_Data folder...
I thought it should be located in the App_Data in the 'domain' projet, shouldn't?
Thanks.
Everything about the database should be located in the 'domain' projet, right?
Ideally it should be located in a data access layer which should be a separate layer of your application than your business/domain layer.
I thought it should be located in the App_Data in the 'domain' projet, shouldn't?
No, since the hosting project is the ASP.NET MVC application, things like App_Data are special folders which have only sense in a web application. So when you define |DataDirectory|MVCLearning.mdf in the web.config of your web application that really means the App_Data folder of the web application.
In domain project you have your business logic and entities. But if you use |DataDirectory| it is placed in App_Data. Your domain project has nothing to do with where data file is stored.
I'm having some problems with deploying my application and while troubleshooting, I came across the Web.Config file in the Views folder. In an attempt to narrow down the possibilities of sources to my problem, I tried to find out the purpose of that ~Web.Config` file but can't really find much information.
So basically my questions are:
What does the Web.config file do in the Views folder of a MVC project?
Is it required?
In Asp.Net webforms, I believe that to use a separate web.config file in a folder, that folder has to be set as a virtual folder in IIS. Is this the case in MVC (i.e. does the Views folder need to be configured as a virtual folder)?
No, you do not need to configure a virtual folder because of this extra web.config file.
The web.config file exists in the Views folders to prevent access to your views by any means other than your controller. In the MVC design pattern, controllers are supposed to route requests and return a rendered view to the calling client.
In other words, your view at www.mydomain.com/MySuperController/AwesomeAction1/SweetPage.aspx should not be directly accessible.
If you peek at the web.config file it actually registers the HttpNotFoundHandler to all paths and verbs:
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
Or, in IIS 7 it might look like
<add name="BlockViewHandler" path="*.aspx" verb="*"
preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
It configures the compiler for the views such as importing namespaces and makes the views folder return a 404.
The web.config file in the views folder is to do some specialized settings you want to apply to pages inside the view folder.
Like config settings like: connection string / appsettings etc.
but that will be applicable to only that folder and rest of the project will pick up the settings from web.config present at the root.
Specially when you use concept of area there will be separate folder for each area containing separate web.cfg file where you can apply separate settings for each area.
That's if you want to override something mentioned in the upper web.config, i.e. if you want to customize something within the scope of the Views folder.
Where is the Web.config supposed to go in an ASP.NET MVC project?
I just ran into an error trying to do this:
_cnstr = System.Configuration.ConfigurationManager.
ConnectionStrings["production"].ConnectionString;
The default MVC template puts the Web.config at the root of the project.
If you go into the properties of a project (the screen with the vertical tabs). Go to settings and try to create an application setting, it will prompt you that you don't have a config file. When it creates the file it does it at the base of the Views folder. So now I have two Web.config files. Is this how it supposed to be?
And I guess I should put my connection string in the "views" web.config to avoid the error.
Thoughts? Is this a bug in the last release of the ASP.NET MVC bits?
UPDATE:
See David's answer
The settings should go into the web.config at the application root. The web.config in the views folder is there to block direct access to the view aspx pages which should only get served through controllers.
(And: I tried creating application settings on my machine, with ASP.NET MVC RC 1 installed, using a newly created mvc web application. They get added to the web.config at the application root.)