19

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.

Saajid Ismail
  • 8,029
  • 11
  • 48
  • 56
  • Another question would be: should the view be allowed to read anything from the configuration? – Jørn Schou-Rode Jun 26 '09 at 15:56
  • Your related question (mentioned in the comment in Haack's answer): You need to open the settings file, in the upper right corner you can set the accessibility (internal vs. public) – Bertvan Nov 10 '10 at 12:22

2 Answers2

32

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

Kieran
  • 17,572
  • 7
  • 45
  • 53
19

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.

Haacked
  • 58,045
  • 14
  • 90
  • 114
  • Thanks Phil. Your explanation makes sense to me. I am implementing a PhotoGaller type service, and have defined default image sizes in the web.config. I want to display these sizes to the user so that they understand that their images need to conform to certain sizes, else it will be resized / rejected. So it is purely for display purposes on my view. I will pass this info from the Controller to my View. Thanks., – Saajid Ismail Jun 29 '09 at 08:19
  • A related question is - why can't I access the strongly typed settings class from my View. I am referring to the settings.settings file, which is usually accessed through MyProject.Properties.Settings.Default.StronglyTypedSetting. I know that I shouldn't do this - I understand this. I want to understand why the compiler won't give me access to it. The settings class is marked as internal, and I am trying to access it from within the same project, so as far as I can tell, there shouldn't be a problem.. – Saajid Ismail Jun 29 '09 at 08:23
  • But, if the information in the settings is of a more global nature - application version number, for example - and the information is to be displayed by one or more master pages, then this is not a controller related responsibility. Would it make more sense to handle this in a HttpHandler or HttpModule implementation? – belugabob Feb 24 '10 at 09:22