What is the equivalent of app.config in MVC 4 - asp.net-mvc

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.

Related

How to serve static files from ASP.NET-MVC 4 app?

This is the first time I've ever developed an ASP.NET app.
I need to serve some static files from my ASP.NET 4 app. The trouble is, I want these files to appear as if they're at the root of the app, when actually they're several folders down. With ASP.NET Core this is easy; you just drop the files in the wwwroot folder, and the server treats these files as if they were at root. But my boss has asked me to use ASP.NET 4, instead of Core, if possible. So I need to figure out how to configure this.
This post and this post both answer the question I have, but I don't understand the answers that are given. Both answers tell me to add some functionality to the "ConfigureStaticFiles" method in my "Startup" class. But I don't have a Startup class.
So, what is the best way to do this in ASP.NET 4? Should I add a Startup class? If I add a Startup class, what do I do with my Global.asax? Or should I approach this some other way?
If you want ALL static files to be served from MVC (rather than IIS) you add this to your web.config
<modules runAllManagedModulesForAllRequests="true">
...under <system.WebServer>
further read #here
Okay.
I was able to use URL rewrites to make the files and folders in a specific subdirectory appear as if they were in the root folder. This answer provides an implementation for that functionality, although my app's architecture necessitated a slightly different configuration.
I don't know if URL rewrites are the best way to accomplish this functionality, but it's what I'm using for now.

MVC Sitecore project - how to add key value settings on web.config

Quiet new to Sitecore hence this basic question to start with...
I've got a MVC Sitecore project up and running. However, I would like to include some simple configuration values on AppSetting of web.config file. From what I read it's not good practice to mess with Sitecore config file.
Could any one please suggest the best practice to add key value on appsettings?
Should it go under app_config?
If yes, then which folder and what naming convention should I use?
And how does Sitecore reads this file on runtime?
In my current project I've removed all app settings from the web.config and included them like so:
<appSettings configSource=".\App_Config\AppSettings.config" />
I then use SlowCheetah (Which can be downloaded here) to change the settings in the AppSettings.config file if I have environment specific settings.
All changes to the sitecore section of the web.config I add through the include files. You can read more on include files in this post by John West

Hosting an MVC and a webforms site on the same IIS7 instance - web.config inheritance

We have a website that was written in classic ASP, then I started to extend it using web forms. These extensions exist in a subfolder of the main folder. Now we've decided we'd prefer to use MVC3. Also, as we'd like to convert all our site to MVC3 over time, we are hosting the MVC code in the application root. I've found some other questions where people have a similar issue to mine, but no solution. The issue is simply that my web forms app can't seem to be stopped from inheriting the web.config settings from the root folder, and as a result, it won't run, it either complains about missing dlls, or complains about running the wrong version of .NET, or complains I need to remove some settings ( which I try and can never get to work right ). The app in the subfolder is also hosting a webservice that is called by our application, and it also runs HTTP handlers to protect our imaging content, so it's got a bit of stuff in it. Do I need to run my MVC site in a subfolder ? Is there any way to have MVC in the folder above a web forms app ? I'd prefer to set things up so they share session data, but that's looking likely to be impossible at this stage...
So to be clear the folder structure is:
<root>
contains asp site and MVC site.
<subfolder>
contains webforms application
</subfolder>
</root>
and my issue is getting the subfolder to run, preferably in the same session as the MVC app.
There is no reason you can't run regular .aspx files on an MVC site. You are correct though, web.config settings are inherited from the parent (chain), but you just add a new web.config in your directory with relevant settings.
What you will have to do is play with the routes, because by default MVC will route all requests into your controller classes. But if you google around its fairly simple to add an exception to the routing.
If you post some of the specific errors we can probably help further.
Oh and do you mean Classic ASP? i.e. not Classic ASP.NET? Because you'll have fun sharing session data between ASP & ASP.NET.

Prevent IIS7/ASP.NET locking directory with images

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.

When hosting an azure MVC app, my web.config's appSettings collection is empty

Using MVC 1.0, and Azure July 2009 SDK
I have an MVC application that appears to be working in the azure test framework, except for the fact that my appSettings collection is empty when running there. (But if I just run the web project on it's own, it's fine.)
Is there something special I need to do to access the web.config from Azure?
Sounds quite odd... I might check that web.config is getting properly copied when you launch under the dev fabric. Browse to your app directory and look for a .csx directory. Under there you should find your web app and hopefully the correct web.config. If not, then the problem is somewhere in packaging. (Make sure that web.config is marked as "content" or "copy always" in the properties in VS.)
Otherwise I'm happy to take a look at the project if it's something you can share. My email address is Steve.Marx#microsoft.com.

Resources