In ASP.NET WebForms you can reference appSettings directly in your markup with this syntax:
<%$ MySettingKey %>
Unfortunately this does not work in ASP.NET MVC because, as MSDN points out, this syntax only works in server controls.
I’ve run into a few situations where I would love to use this syntactic sugar in an ASP.NET MVC view (WebFormsViewEngine). Does anyone know if there is a way to get this working?
Seems like we might be able to derive from WebFormsViewEngine and add this as a feature, perhaps?
Not very clean but in an ASP.NET MVC View you could actually write this:
Which will effectively print whatever the value you have in appSettings:
Oh and there won’t be a VIEWSTATE tag added to your page 🙂
Now to the point: I will strongly discourage you doing something like this MVC. It is not the View’s responsibility to pull the data to show, it’s the controller that needs to pass it. So I would make MySetting a property of the ViewModel which will be populated by the controller and passed to the view to be shown.
And in the View:
or even shorter with the new syntax introduced in ASP.NET 4:
UPDATE:
Yet another alternative if you think that MySetting is not a property of the ViewModel (like some css name or similar) you could extend the HtmlHelper:
And use it like this: