title: Should I inject services into my MVC views?
body: <p>We're working on some kind of Cloud CMS using ASP.NET MVC technology, and have found some obstacles on the way. There is a number of parameters user could change thru the control panel that we need to end up in Views. For example, Facebook application id to initialize the Facebook JS API. Or additional text to be shown on the page. Or background picture. For now we're not using DI to transfer this parameters, instead we're adding them to the ViewModel, but this ruin the ASP.NET MVC way of working with models (e.g. form validation, bindings etc.)</p>
  
  <p>It looks like that using DI to inject services for providing parameters, texts and pictures could make my views less dependent on controllers specific, and there is even some Microsoft technique to do it <a href="http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-dependency-injection#Exercise2" rel="noreferrer">http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-dependency-injection#Exercise2</a>. However, there are a lot of answers on forums against injecting services into Views using DI.</p>
  
  <p>So the question: what is a right way to inject some services into Views? Or I shouldn't do it at all and something is wrong in the application design?</p>
  
  <p><strong>UPDATE</strong>: some real code examples (now we're using Model to inject the services)</p>
  
  <p>Injecting texts from database (they have to be user-editable, as it is CMS):</p>
  
  <pre><code>&lt;div class="steps"&gt;@Html.Raw(Model.Texts["Main", "Step2"]&lt;/div&gt;
  </code></pre>
  
  <p>Injecting translations from database (actually, it is localization):</p>
  
  <pre><code>&lt;div class="gonfalon"&gt;@Model.Translations["Title_Winners"]&lt;/div&gt;
  </code></pre>
  
  <p>Injecting parameters (from database, could be request-specific; for example, if the site has different domains, facebook application should be per-domain):</p>
  
  <pre><code>Facebook.Initialize(Model.Parameters["FbApplicationId"], Model.Parameters["FbApplicationSecret"]);
  </code></pre>
  
  <p>The problem of current approach is that this code has taken from contest mechanic. It is definitely out of contest business scope to deal with custom texts, translations or facebook application Id. Also it ruins the Model as model models not actual business domain but deals with a lot of things actually belongs to View (like translations and custom texts)</p>
  
  <p><strong>UPDATE 2</strong>: Have modified the snippet from the answer below to be a bit more generic:</p>
  
  <pre><code>public static class WebViewPageExtensions 
  { 
      public static I ResolveService&lt;I&gt;(this WebViewPage page) 
      { 
           return DependencyResolver.Current.GetService&lt;I&gt;(); 
      } 
  } 
  </code></pre>