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
Related
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" />
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 downloaded the source code of the MVC 3 to learn How it runs .
Many people said that the MVC interceptes Http’s requests by the UrlRouting Moudle Class.
I know when you custom a HttpModule ,you need to register it like that:
<system.webServer>
<modules>
<add name="test" type="WebApplication2.MyModule1,WebApplication2"/>
</modules>
</system.webServer>
So Asp.net mvc application Web.config file Should be have the configuration section:
<add name="UrlRoutingModule"
type="System.Web.Routing.UrlRoutingModule,..." />
But I can't find it int the web.config file,when I create a new Asp.net MVC application .
Someone said the IIS 7 would automatically added it .
When to IIS7 add the configuration section?
How the IIS7 Difference it is a MVC application or WebForm?
you have a few questions in there. The web.config is located in the root of your start up project when you open it in Visual Studio, else if you 'explore' the application within IIS, this should take you to it also.
Not all modules are included in the web.config, some are also in the machine.config. This is usually in C:\WINDOWS\Microsoft.NET\Framework\<version>\CONFIG
Hope that helps.
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.
Hi I am installing Umbraco to the root directory of my website. And also I want to install my wordpress blog to a subfolder in the root directory.
However any request to the subfolder is being intercepted by Umbraco and it is showing that document URL cannot be found. Is there a way to make Umbraco ignore certain subdomains?
You can tell Umbraco to ignore certain paths or folders in the web.config.
You need to update the following entry in the appSetting section of the web.config:
umbracoReservedPaths
Add the subfolder you wish Umbraco to ignore in there.
i.e.
<add key="umbracoReservedPaths" value="/umbraco,/install/,/wordpress/" />
There are more details on the Umbraco web.config settings in on our.umbraco.org
it's useful for binding too:
<add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/http-bind/" />
~/http-bind/
Thanks Tim Saunders
You can also use umbracoReservedUrls to tell umbraco to ignore specific pages:
<add key="umbracoReservedUrls" value="/mypages/regular-page.aspx" />
This can be set up with in IIS by setting up the blog as a separate application with in the umbraco site.
However if you are trying to make a sub-domain there is no reason why this should be in the same site, I would say it is best to create a separate site with in IIS or purchase another hosting account to host the blog separately