ASP.NET MVC and two Web.config files - asp.net-mvc

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.)

Related

how to set default document in web.config file in ASP.NET MVC application..?

I'm working on ASP.NET MVC application it is working fine now i want to host it on IIS server, I'm doing this as generic procedure when I'm opening it on browser that time it showing all directories list what now how can set my default action in web.config file.
You don't set a default document with MVC. There's no such thing as documents in MVC, let alone a default one. If you're getting a directory listing instead of your app being loaded, then there's some problem with your setup. Most likely, you don't have it as an actual application in IIS, but just a virtual directory

Define ashx file in MVC doesn't work

I define a ashx file in MVC4 project in content folder so it doesn't work ?!!
So i have several questions?
1- Can we define ashx file in MVC or it isn't possible?
2-If we can define how can we do that?
Best regards
You can definitely use an .ashx file in MVC. An MVC app is an ASP.NET Web app. You should add the .ashx in the root folder (not the content folder), or in a normal subfolder, as you would in an ordinary ASP.NET web project.

Two web.configs?

ASP.Net MVC applications has two web.configs. One in the root folder and one in the Views folder. Why?
From Pro ASP.NET MVC 2 book:
/Views/Web.config:
This is not your application’s main
Web.config file. It just contains a
directive instructing the web server
not to serve any *.aspx files under
/Views (because they should be
rendered by a controller, not invoked
directly like classic Web Forms *.aspx
files). This file also contains
configuration needed to make the
standard ASP.NET ASPX page compiler
work properly with ASP.NET MVC view
syntax.
One reason is to simplify your views and your pages. You can put the compilation or even the masterPageFile declaration from your views in this web.config, for example.
Phil Haack did a great post on this -> http://haacked.com/archive/2009/08/04/views-on-a-diet.aspx

Routing issue when using ASP MVC 2 areas

I am using asp mvc 2 areas. I am trying to set up the project such that when the web site is launced it calls one of the controllers in my areas instead of the default home page.
When I update the routes in the Global.asax file, it doesn't seem to work.
It is giving me an error since it is looking only in the views directory and not in the Areas folder. How can i force it to look into the Areas folder too?
Have you created a custom ViewLocator as described here: http://blog.codeville.net/2008/07/30/partitioning-an-aspnet-mvc-application-into-separate-areas/

ASP.NET MVC - Missing "Convert to Web Application" option

I created a new MVC project and added some webforms pages to it in an effort to start adding new pages to my app using MVC and eventually port the old pages over as well. Everything is building and working correctly but I did notice that I don't have the "Convert to Web Application" option when right clicking an aspx file. And I think its not regenerating my designer files when I change the controls on a page.
My guess is that the ProjectTypeGuid is wrong or in the wrong order. Can someone confirm?
Old (Webforms) project file
<ProjectGuid>{4F95C3D9-228E-4BD5-9840-46224BA3EBA7}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
New (MVC) project file
<ProjectGuid>{A4690D3F-695B-4BF4-93B7-EA5B17793051}</ProjectGuid>
<ProjectTypeGuids>{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
This is so wrong on so many levels but I am going to answer you anyways in the hopes I might get an uptick or something. You have two routes with this. First route which I use all the time is put your mvc applications in their own projects. When your deploying the site drop the webforms application first, and then make a folder in that webforms application and put your mvc application into that folder. That should work like a charm for you. If you insit on having webforms and MVC Framework in the same project, then don't drop your webforms into the view folder. Create its own folder because you can not directly access your aspx pages from the views folder without making modifications to the web.config. Hope this helps.
MVC is available as a Web Project only. The VS2005 style Web Site is not supported.

Resources