Is it possible to do away with the web.config file in the Views folders of an MVC application?
Due to a bug in the encryption feature of Web Deploy I need to just use a single web.config file in the root of the site.
see: MSDeploy automatic encryption of connection strings, key not found in dictionary
Related
I have only few projects on my bag. In a previous desktop application when I wanted to store and use some application specific information I was using the app.config file.
Now I work on ASP.NET MVC 4 application and again I want to store some application specific information but this time I'm not sure. I have the web.config file which seems like a good place for this purpose but I'm not sure if it's the right place to store custom information there.
What is the right approach to do this? For example I want to save and extract path to directory on the file system where I'll save all my files. In ASP.NET MVC 4 what/where is the right place to do that?
The web.config file is what web application use where a desktop application uses app.config, and it's a good place to put application specific information.
You can add keys to the <appSettings> tag, and use them in the application just as you would if you put them in that tag in the app.config file. Example:
string path = ConfigurationManager.AppSettings["DataPath"];
Web.config is for ASP.NET and ASP.NET MVC; app.config is for desktop applications and DLLs.
If you need to storie any kind of parameter for your application that you can change without having to recompile, Web.config is the place to go.
Web.config is the web application version of app.config. Web.config seem like a good place for the configuration settings you're talking about.
Sometimes when you have a dll that had reference to desktop app.config, and when you import that MVC application you need to change the configuration data that used to be in app.config now to web.config.
need your help with this one, i'm running azure deployment on my localhost and get the webpage without css and javascript, when i look in fiddler i see 500 status and the following error: "It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS" on each request for javascript or css file, i already enabled static content on windows feature and it didn't help, i'm using iis 7.5 on windows professional 64 bit with azure sdk 1.7, tried anything, and nothing helped, can't change the site to web application cause the website only exist on iis while i'm running debug, any idea what to do?
Could it be that the directory from which you're serving JS/CSS files contains a web.config file? If this is the case this will be causing the problem.
Your web.config contains an element which can only be defined in a real web application (in the root of your application for example- or in the machine.config. To fix this issue you'll need to open the machine.config (C:\Windows\Microsoft.Net\Framework\v4.0.30319\Config\machine.config) and look at each configSection defined here that is also included in your web.config (in the folder containing the JS/CSS files).
If the configSection in the machine.config contains the allowDefinition attribute with MachineToApplication as value this means you cannot use that configSection in a web.config that isn't located in a web application. Meaning in your case you'll need to remove that configSection from the web.config in the folder containing the JS/CSS files.
If you're using ASP.NET MVC this is probably your Content folder.
I have an ASP.NET webapp using the ASP.NET MVC 2 framework. It allows users to upload files to an uploads folder. The issue occurs when an image within a sub-folder is accessed by a web browser:
http://mywebapp/uploads/image_gallery/sub_folder/image.jpg
The uploads folder is static and can't be modified by users, but anything below it is intended to be modifiable.
In the above example, the image_gallery folder becomes locked because w3wp.exe appears to create a handle on the sub_folder directory (using process explorer by sysinternals). I am still able to rename the sub_folder directory and the handle seems to stay with it after a rename, but i can't rename the parent folder (image_gallery in this case).
I can still browse within the folder and view other images and files etc. But can't rename the parent folder.
As this is using the MVC 2 framework i've put in an exclusion for the uploads folder like so:
routes.IgnoreRoute("upload/{*pathInfo}");
into global.asax, so i'm assuming that ASP.NET is serving up those images directly (outside of the MVC framework)
So I guess my question is, is there any way to prevent IIS from putting a handle on specific directories or forcing it to remove a handle? Is the MVC 2 framework doing something tricky even though i have the ignoreroute specified?
Thanks in advance for any tips!
I had the same problem, and after much investigating I've found the culprit:
Web.config
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
Setting this option (which makes all http modules run on ALL files, static ones included) to false fixed it for me.
I believe, directory handle gets created in worker processor because ASP.NET typically watches the file system for changes - this allows it to respond to change/addition of config files, new dlls etc.
I suggest that you move from the concept that users can control physical directory structure on the web server. Typically, what user can create is an logical directory structure but physical structure would be controlled by your program logic. Your logic will store the logical structure created by user and its mapping with actual physical structure on the web server.
I understand this question can be general but, specifically with regards to ASP.NET MVC, what's the best way to keep passwords locally but not in git/svn? My database connection string is currently in web.config -- is it best to somehow include a file in web.config that isn't in git? What do you folks do?
I use Windows Auth with my databases, so the connection string contains a server name but not a username/password.
For Machines where I can't use Windows Auth, I use web.config transforms and add the web.dev.config to my .gitignore.
Regarding Git, I would use a filter driver in order to build the right web.config out of:
a web.config.template file,
an external (encrypted) source where to look for the password.
On every checkout, the 'smudge' script would be the right web.config content, that way:
web.config remains private (only visible in your working tree)
common parts of the web.config which don't change often and are public information remain versioned in web.config.template.
the password, even encrypted, don't get replicated from repository to repository.
put the web.config in your .gitignore file. The web.config will not change very often.
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.)