Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access application settings from ASP.Net MVC View

In an ASP.Net MVC 1.0 applicati0n, is it possible to access the application settings (MyProject.Properties.Settings.Default.*) from inside my View (aspx page)?

I've tried but the intellisense and compiler don't like it. It says that it is inaccesible due to the protection level.

like image 929
Saajid Ismail Avatar asked Jun 26 '09 15:06

Saajid Ismail


People also ask

Where are settings in asp net core MVC?

Most ASP.NET MVC and Web API apps store their settings in the configuration file's appSettings or connectionStrings elements. Some also use custom configuration sections that can be mapped to a settings class.

Where are .NET application settings stored?

settings is located in the My Project folder for Visual Basic projects and in the Properties folder for Visual C# projects. The Project Designer then searches for other settings files in the project's root folder. Therefore, you should put your custom settings file there. If you add a .

How do you read app settings properties in the code?

To access the setting's value in your code, get the value by indexing into the AppSettings property. The AppSettings property makes it easy to obtain data from the <appSettings> element of your configuration file.


2 Answers

I had a similar issue to Saajid Ismail where my settings were in the namespace.Properties.Settings.Default.Setting they were there as they are strongly typed..

To make them accessible I simply had to change the access modifier enter image description here

like image 59
Kieran Avatar answered Oct 19 '22 19:10

Kieran


Your View should only be responsible for rendering data given to it by the Controller. It's responsibility is for layout. So I would recommend passing the Application data to the view from within your Controller action.

Having said that, the technical answer to your question is that ViewPage derives from Page, so you can simply do this:

<%= Context.Application["setting"] %>

But again, I don't recommend it.

like image 35
Haacked Avatar answered Oct 19 '22 19:10

Haacked