title: How to access resources in ASP.NET Views folder?
body: <p>Resources in Views folder are CSS, Images used inside the View, now I know it is different then what it is meant for, we are already putting everything in Contents folder but I am looking for better way to manage it.</p>
  
  <p>We have a very big MVC 4 and there are many areas and each area has many views. To maintain everything we created similar folder structure in contents folder to distribute CSS and images accordingly.</p>
  
  <p>But the problem is, referencing each resource on view. It becomes difficult to keep track and here is the folder structure.</p>
  
  <ul>
  <li>Area
  <ul>
  <li>App
  <ul>
  <li>Controllers</li>
  <li>Views
  <ul>
  <li>Projects
  <ul>
  <li>Search.cshtml</li>
  </ul></li>
  <li>Users
  <ul>
  <li>Account.cshtml</li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul></li>
  <li>Content
  <ul>
  <li>common.less</li>
  <li>App
  <ul>
  <li>app.less</li>
  <li>some-button.png</li>
  <li>projects.less</li>
  <li>users.less</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>Now problem occurs when we have too many resources and cross referencing is difficult, as we make user interface changes, new views come in picture as project grows.</p>
  
  <p>I would like to have resources as following format,</p>
  
  <ul>
  <li>Area
  <ul>
  <li>App
  <ul>
  <li>Controllers</li>
  <li>Views
  <ul>
  <li>Projects
  <ul>
  <li>Search.cshtml</li>
  <li>search.less</li>
  <li>some-button.png</li>
  </ul></li>
  <li>Users
  <ul>
  <li>Account.cshtml</li>
  <li>account.less</li>
  <li>some-button.png</li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>How can I do easily?</p>
  
  <p>Each of my controller is derived from a common AppController, in which we validate user authentication and roles. I have implemented CustomActionInvoker to find views as follow.</p>
  
  <pre><code>private string defaultViewName = "";
  
  protected virtual object GetDefaultViewModel() {
      return null;
  }
  
  public virtual ActionResult DefaultView() {
  
      try
      {
          var view = this.View(defaultViewName);
          if (view == null)
          {
              return HttpNotFound(defaultViewName + " View not found");
          }
          ViewBag.Title = defaultViewName;
      }
      catch
      {
          //Utils.Log(ex);
          //throw;
          return HttpNotFound(defaultViewName + " View not found");
      }            
  
      return View(defaultViewName, GetDefaultViewModel());
  }
  
  protected override IActionInvoker CreateActionInvoker()
  {
      return new DefaultViewInvoker(base.CreateActionInvoker(), this);
  }
  
  internal class DefaultViewInvoker : ControllerActionInvoker {
  
      private IActionInvoker parent;
      private CastingWebController wc;
  
      internal DefaultViewInvoker(IActionInvoker parent, CastingWebController wc)
      {
          this.parent = parent;
          this.wc = wc;
      }
  
      protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)
      {
          ActionDescriptor ad  = base.FindAction(controllerContext, controllerDescriptor, actionName);
          if (ad == null) {
              wc.defaultViewName = actionName;
              ad = base.FindAction(controllerContext, controllerDescriptor, "DefaultView");
          }
          return ad;
      }
  
  }
  </code></pre>
  
  <p>This works well, it distributes the CSHTML file even if we have no action method on controller. However this does not work with CSS and images.</p>
  
  <p>Example,</p>
  
  <pre><code>/App/Projects/Search
  </code></pre>
  
  <p>is easily processed and served by, we don't see raw CSHTML download anyway.</p>
  
  <pre><code>App/Views/Projects/Search.cshtml
  </code></pre>
  
  <p>I need little help in accessing</p>
  
  <pre><code>/App/Projects/Search.css
  /App/Projects/some-button.png
  </code></pre>
  
  <p>at path</p>
  
  <pre><code>/App/Views/Projects/Search.css
  /App/Views/Projects/some-button.png
  </code></pre>
  
  <p>As these are not CSHTML, view finder does not find them and also I need to serve them as static download, anything apart from cshtml,aspx and having a registered mime type.</p>
  
  <p>And to add on it, we also have DisplayModes with Mobile and Tablet that makes it even difficult to manage resources.</p>