title: MVC Change ViewName on the fly
body: <p>I have a project where i want to show different views to Desktop e Mobile users, mostly for better experience and speed. Right now i'm using <strong>51degrees</strong> to tell me if the request comes from a mobile or desktop user and then in my <strong>Action Filter</strong> i will change the view name from "Index" to "Index.Mobile", the thing is i'm getting some problems with Views where i don't want to use a Layout at all.</p>
  
  <p>This is my Method:</p>
  
  <pre><code>public override void OnActionExecuted( ActionExecutedContext filterContext )
  {
      var result   = filterContext.Result as ViewResult;
      var view     = filterContext.ActionDescriptor.ActionName;
  
      //IsMobile is filled on my OnActionExecuting method
      var IsMobile = filterContext.Controller.ViewBag.IsMobile;
  
      if( result == null ) return;
  
      string viewName;
      string masterName;
  
      if( IsMobile )
      {
          viewName   = view + ".Mobile";
          masterName = "_Layout.Mobile";
  
          //Check if view exists
          var viewResult = ViewEngines.Engines.FindView( filterContext.Controller.ControllerContext, viewName, null );
  
          if( viewResult.View == null )
          {
              viewName = view;
          }
      }
      else
      {
          viewName   = view;
          masterName = "_Layout";
      }
  
      result.ViewName   = viewName;
      result.MasterName = masterName;
  }
  </code></pre>
  
  <p>What happens is, here i set a value for the <strong>MasterName</strong> or <strong>Layout</strong> and at two specific <strong>Controllers</strong> i need to set it as <strong>null</strong> but it still fetches the <strong>Layout</strong> i set before.</p>
  
  <p>What i need is, this method will set the view and layout name and if need i will set the Layout as null inside the view i loaded.</p>
  
  <p>Can that be done ?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried to implement another method, <a href="https://stackoverflow.com/questions/17083172/change-the-views-path-on-the-fly?answertab=votes#tab-top" title="LINK">Link</a>, but i couldn't find a way to change the Layout view, only the action.</p>