what is the purpose of web.config in views folder? [duplicate] - asp.net-mvc-3

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.

Related

Reduce security from views folder MVC

In my views folder i want to use templates from jsrender but when i try to access anything from the Views folder it gives me 404 and 500 error as response not found.
What i just want to do is to access *. HTML file from the Views folder.
What should be the change in Web.Config of Views?
Thanks
What i just want to do is to access *. HTML file from the Views folder
No, you can't. The Views folder has a special meaning in ASP.NET MVC and there's a handler which forbids access to any file in it. You just have to put your templates somewhere else. A ~/Templates folder doesn't seem like a bad idea.

Problems hosting multiple ASP.Net MVC3 applications in an IIS7.5 website

Some specs:
IIS 7.5
ASP.Net MVC3
I have three MVC3 applications I would like to host:
Project A
Project B
Project C
I want to get the URLs to be like this, respectively:
http://domain/
http://domain/ProjectB
http://domain/ProjectC
So in IIS, I have the default website, which points to the folder for ProjectA. I then added two Applications to it, one pointing to ProjectB, and one to ProjectC, and their respective folders on the hard drive. I am storing the sites in C:\sites. Each project has a folder in C:\sites where the application's files reside. As you can see, the sites are not nested at all on the server.
When I load http://domain/ProjectB, it is trying to load configuration values from the ProjectA web.config. Obviously this is not what I want, and I'm not that awesome at server configuration. I got it working by wrapping most of the ProjectA web.config in a location tag like so:
<location path="." inheritInChildApplications="false" >
<!-- Lots of web.config stuff for ProjectA -->
</location>
I'm not really a fan of this, it feels wrong. To me, it seems like a nested Application in my site in IIS should not even know about the parent site. What am I doing wrong? I'm sure it is something really small. I would have made ProjectA an Application, but I don't want the alias on it, as I want http://domain/ to pull it up for the users.
Again, I am not that knowledgeable about server stuff, it just isn't fun to me, hence why I'm asking y'all!
Thanks!
That is how it works. By default, asp.net applications inherit their parents web.config, and you either need to override those values in your lower web.config or wrap it in an inheritInChildApplications="false" attribute like you did above.
One possible option is if you can make your home page at the root of the site NOT an asp.net application. If you can't, you're stuck doing this.
on goDaddy you can create iis virtual directories with "set application root", which then makes each director with "set application root" set act as a root directory (just as you want). The user don't notice anything. So you could look and see if you have that option?

Razor file loads in on application but not in the other on same server

I have two applications setup in IIS7.5. MVC 3 is installed. One application serves Razor files fine. A separate application was recently created that will not serve Razor files. I get the following error when accessing the file using the full filename (file.cshtml):
This type of page is not served.
Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension '.cshtml' may be incorrect. Please review the URL below and make sure that it is spelled correctly.
When trying to access the file without an extension (/path/file/) I get a 404 error.
I have searched for this problem but haven't found a solution where it works with one application but not another on the same server.
Both applications are using the same App Pool.
Web.Config files are identical.
Do both sites have a CSHTML file in the root of the application? Since the WebPages framework (which is what is used when you request a CSHTML file directly) has a significant impact on your site's performance if you aren't using it, we only start it if there's a CSHTML file in the root folder of your site (i.e. ~/Foo.cshtml). If you don't have any CSHTML files in your root, you can also add a web.config entry to set an appSetting:
<configuration>
<appSettings>
<add key="webpages:Enabled" value="true" />
</appSettings>
</configuration>
If you're confused by my answer, it would help if you edited your question to add information about the file layout of the two apps. Then I can add some concrete examples to try and clarify things :).
Hope that helps!

Resource cannot be found for content inside of theme folders

My project has a feature to override views and content, if they exist, from a themes folder. The path to the themes folder will vary by site, so my folder structure looks like this:
Themes\
SiteA\
Content\
Images\
logo.png
screen.css
Views\
Home\
Index.cshtml
I am able to successfully override the default view with the site's custom one. However I am unable to access anything in the Content folder for the theme. If I access the file directly at http://localhost:port/Themes/SiteA/Content/screen.css, the resource cannot be found. I also get this error when I try to access anything in the images folder. A co-worker was able to do this for a separate project last year, but I cannot find any notable changes to web.config or other files that would make it work. Any help is appreciated!
A co-worker found the problem. When I setup the Themes folders, I copied web.config from Views, which has this option:
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
We changed the path attribute's value from * to *.cshtml and the files are now accessible.
I'm not able to comment so I'm posting here. Do you have a route that matches the url? Try the RouteDebuger to see what route is hit.
Have you checked the permissions on the underlying folder in the file system? In my experience that's often the cause of being unable to access a resource through a URL - the credentials used by the web site may not be granted access to the underlying resource

Access Rules created by WSAT are not enforced

I'm trying to implement roles in my site.
There are several projects in the solution, one of which is a web application.
In that web application, I'm trying to use WSAT to create three roles. There are many folders for the application. I've used WSAT to define role based access rules for each folder.
However, when I debug and navigate to those pages, they do not redirect to a login and show me the protected page.
There are web.config files in each folder.
Why would the system not enforce these rules?
My web.config file has:
<roleManager enabled="true" defaultProvider="AspNetSqlProvider" />
I've tested the connections in WSAT and they work.
Any ideas?
try
<roleManager enabled="true"/>
Also, please edit your question to provide the web.config from one of your subdirectories.
Yes, I fixed the problem.
A previous developer had cleared all the HTTP modules. Since all the modules were disabled, the authentication module wasn't part of the asp.net pipeline.

Resources