title: Working with IViewLocationExpander in mvc
body: <p>I want to render views from a custom location, so for that I have implemented the 
  <code>IViewLocationExpander</code> interface in a class. I have registered the same class in <code>Startup</code> class as follows.</p>
  
  <p><strong><code>Startup</code> Class</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>public void ConfigureServices(IServiceCollection services)
  {
      … 
      //Render view from custom location.
      services.Configure&lt;RazorViewEngineOptions&gt;(options =&gt;
      {
          options.ViewLocationExpanders.Add(new CustomViewLocationExpander());
      });
      … 
  }
  </code></pre>
  
  <p><strong><code>CustomViewLocationExpander</code> Class</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>public class CustomViewLocationExpander : IViewLocationExpander
  {
      public IEnumerable&lt;string&gt; ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable&lt;string&gt; viewLocations)
      {
  
          var session = context.ActionContext.HttpContext.RequestServices.GetRequiredService&lt;SessionServices&gt;();
          string folderName = session.GetSession&lt;string&gt;("ApplicationType");
  
          viewLocations = viewLocations.Select(f =&gt; f.Replace("/Views/", "/" + folderName + "/"));
  
  
          return viewLocations;
      }
  
      public void PopulateValues(ViewLocationExpanderContext context)
      {
  
      }
  }
  </code></pre>
  
  <p>And, finally, my application’s views are organized as follows:</p>
  
  <p><a href="https://i.stack.imgur.com/HyOhW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/HyOhW.png" alt="View folder structure"></a></p>
  
  <p>My issue: If I access the <code>Views/Login</code> view from the <code>ViewsFrontend</code> folder from the following URL:</p>
  
  <pre><code>http://localhost:56739/trainee/Login/myclientname 
  </code></pre>
  
  <p>But then immediately change the URL in the browser to:</p>
  
  <pre><code>http://localhost:56739/admin/Login/myclientname
  </code></pre>
  
  <p>In this case, it still refers to the <code>ViewsFrontend</code> folder, even though it <em>should</em> now refer to the <code>ViewsBackend</code> folder, since URLs beginning with <code>trainee</code> should refer to the <code>ViewsFrontend</code> folder, while those beginning with <code>admin</code> should refer to the <code>ViewsBackend</code> folder.</p>
  
  <p>Further, after changing the URL in the browser, it only calls the <code>PopulateValues()</code> method, but not the <code>ExpandViewLocations()</code> method.</p>
  
  <p>How can I reconfigure this class to work for this other folder?</p>