Access application settings from ASP.Net MVC View
Asked Answered
G

2

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.

Guardant answered 26/6, 2009 at 15:50 Comment(2)
Another question would be: should the view be allowed to read anything from the configuration?Hypethral
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)Peephole
A
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.

Anselm answered 26/6, 2009 at 17:17 Comment(3)
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.,Guardant
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..Guardant
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?Lachlan
B
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

Bobker answered 20/9, 2011 at 2:4 Comment(0)
A
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.

Anselm answered 26/6, 2009 at 17:17 Comment(3)
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.,Guardant
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..Guardant
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?Lachlan

© 2022 - 2024 — McMap. All rights reserved.