views:

950

answers:

1

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.

+4  A: 

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
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
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
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