Where should I store configuration information in ASP.NET application? - asp.net

I have an ASP.NET application.
It resides within another ASP.NET application.
I put my DLL in the other apps bin folder and have a subdirectory with my aspx files.
I can't edit the main app's Web.config.
So where can I store configuration settings?
For example, this is an app that will get deployed to various clients. I want to store the client name used for display in the headers and such. And the location of a logo file.

You can put a web.config file on the folder your ASPX files reside and .NET will take care of nesting them and aggregate its contents.

You can also just create a Settings.config XML file and put it wherever you want (the .config extension will prevent it from being viewable through an HTTP request), then wrap it with a Settings class and talk to that Settings class from your application.

You can use Settings.Settings to store it. This example might help. You can have multiple settings files and access any of them, just as you would for any other class.

Related

Is there a way to tell a View in ASP.NET where to look for a Web.Config?

I've recently began a new project and I've decided to try to arrange the folders in a basic ASP.NET MVC project by feature (i.e. one folder would contain all Account files Models, View and Controller and another folder would contain Home Model, View and Controller etc...) instead of the default ASP.NET template. With this being said I quickly realized when I moved my views my controller was looking under the views folder. I fixed that but then my views were looking for the Web.Config and I fixed that by copying the views folder Web.Config into that folder. This is a solution but I want to know if I can point all my views to one Web.Config without having to have them all in the same folder.
Every folder in ASP.NET web application / website can have its own web.config file. Having said that, it is also necessary that the root folder of your application must have a web.config file based on which the application / website will be configured in the hosting environment.
Therefore, if you want to use different settings for different folders, you can place a separate web.config file with settings specific to folder in the any of the application folder.

Limiting file download based on access level on asp.net

I have a web app made in asp.net mvc3. There is a facility to upload and download files using the application. Uploaded files will be stored in some folder under web root. I want allow downloading files to those who have access to the files only. No one should be able to download the file by directly pasting in the file URL.
I use shared hosting with limited IIS access. So what would be the best way to achieve this?
How are you storing the data on the access rights currently? It sounds like you are not going to be able to make use of IIS to control access to your files and will have to handle it yourself.
As this is the case, rather than link to the file directly you should store the files outside of your web root and then handle requests coming in for files through ASP.NET MVC using a GET method. At that point you can check the user's credentials, and if they have access you can serve the file.
I'm not too familiar with it, but it looks like ASP.NET MVC makes serving up files very easy with the ability to return a FileContentResult, supported by the Controller.File method (documentation here).
This blog post looks like a great start, and you would just need to insert your credential-checking logic into the Get method.

share configuration between console application and asp.net web application

I have a web.config file defined in my asp.net web application. I have many different settings configured there.
I have another project, this time a console application. I'd like to read several configurations from my web.config file. How can this be done?
Thank you
Check out this article.
Does exactly what you want.
Basically you "delegate" sections of your config file to another file.
In your case you would delegate sections of your config file in your console app, to read the settings from the web.config in your web app.

Editing resource files without recompiling ASP.NET application

I'd like to enable the resource files to be editable after deployment. I read this post which suggests that this is possible, but I can't seem to figure out what settings I need to change to enable this.
I have added the App_GlobalResources folder to my ASP.NET 3.5 "Web Application" and added a resource file to this folder. I assume that the Build Action for this file needs to be changed, but no matter what I change the Build Action to, I cannot achieve the above stated functionality.
The App_GlobalResources folder and the resource file are copied into the bin directory. Once deployed, any edits to the .resx file are not being displayed.
Any ideas?
You can achieve this, and I just did it.
Select the resource file in your project. Change the Build Action to content. Make sure that the Copy to Output Directory setting is turned OFF. When you deploy your project, the App_GlobalResources directory and your .resx file will get copied to the root of your web site. You can modify the .resx file and your live site will recognize the changes.
A Web Application project is different than a Web Site project. I doubt you can achieve what you want with a Web Application project. You might check out this post:
ASP.NET Web Site or ASP.NET Web Application?
Resources are generally meant to be static. They are items such as images, strings, and files that your program consumes and can rely on being present, (and therefore can be strongly typed in the case of strings/RESX files). The reason for using resources is simply to bundle them in with your DLL's so that distribution and referencing the resources becomes much easier.
Editable at runtime suggests you might want to use project or user Settings, or maybe a database? If you need to use RESX files you might need to write code to read/write them.

web.config in nested folder

I am trying to install an app inside of another web app. I have my .aspx pages and some code that I was putting into the main app's app_code folder. I've added my own web.config file for my connection string and such but I think there's a conflict. So my question is a two parter. First, what is the best way to install an app inside of another app, i.e should I use the main apps app_code folder or add my own, and second, would there be a conflict with the two web.config files. I was under the impression that the files pulled from the most specific web.config file. It appears there is a problem with my security and I am unable to access my file. I was attributing this to the two web.config files,
thanks.
If the nested application has had its folder turned into an application (right-click on it in IIS, Properties, and on the "Application" tab, "Create" a new application), you should put the code in the local App_Code folder:
- \RootFolder // Root of website
|- \App_Code // App_Code at root
|- \NewApplication // Seperate application in IIS, has "web in a box" icon in IIS
| |- \App_Code // App_Code of new application
If the nested application isn't a true application (in the IIS sense), then you will need to have the code files in the root App_Code folder.
This also has a bearing on your web.config - if the nested application is a true application, then you'll be able to have a full web.config at the level you want - however if it's not an IIS application, then there are limitations as to what you are able to put in subsequent web.configs - some elements are only allowed in the web.config at the application root, and can't be overridden by other settings.
What's the actual error you are seeing?
Regarding your first question, I would rather have them deployed on different folder. And second, if you have, for instance, a web site inside the default web site, you will have both web.config, but the more specific will override some of the attributes of the web.config from the default web site, but the ones that are not override will be there, (ie, HTTPHandlers, HTTPModules, the site will try to load those, so you will need to add the remove tag inside the HttpModules to remove them).
Hope this clarify your question

Resources