title: MVC - Index (zero based) must be greater than or equal to zero and less than the size of the argument list
body: <p>I was following <a href="https://stackoverflow.com/questions/24341336/is-it-possible-to-access-mvc-views-located-in-another-project">this</a> <strong>Mohsen Esmailpour</strong>'s answer, to build a sample project to load views and controllers from a class library using custom view engine.</p>
  
  <p>Here's what I did,</p>
  
  <ol>
  <li>Add a Asp.Net MVC project</li>
  <li>Add a class library. Add references to 
  
  <ul>
  <li>Microsoft.AspNet.Razor</li>
  <li>Microsoft.AspNet.WebPages</li>
  <li>Microsoft.Web.Infrastructure</li>
  <li>Microsoft.AspNet.Mvc" </li>
  </ul></li>
  </ol>
  
  <p>Reference class library in MVC project. 
  3. Add a "Controllers" folder and added "MyViewController.cs" class</p>
  
  <pre><code>public class MyViewController : Controller
  {
      public ActionResult Index()
      {
          return View();
      }
  }
  </code></pre>
  
  <p>View engine (which should be causing the problem), I am not entirely sure what's happening here,</p>
  
  <pre><code>public class CustomViewEngine : RazorViewEngine
  {
      public CustomViewEngine()
      {
          MasterLocationFormats = new string[]
          {
              "~/bin/Views/{1}/{0}.cshtml",
              "~/bin/Views/Shared/{0}.cshtml"
          };
          ViewLocationFormats = new string[]
          {
              "~/bin/Areas/{2}/Views/{1}/{0}.cshtml",
              "~/bin/Areas/{2}/Views/Shared/{0}.cshtml"
          };
      }
  }
  </code></pre>
  
  <p>Route.config for the Mvc project,</p>
  
  <pre><code>public class RouteConfig
  {
      public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
          routes.MapRoute(
              name: "Default",
              url: "{controller}/{action}/{id}",
              defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
          );
  
          routes.MapRoute(name: "Route2", url: "{controller}/{action}/{id}",
              namespaces: new[] { "Custom.Views.Controllers" },
              defaults: new { controller = "MyView", action = "Index", id = UrlParameter.Optional });
      }
  }
  </code></pre>
  
  <ol start="4">
  <li>Add Views folder and added "MyView.cshtml" (Views/MyView/Index.cshtml), also I set the Properties of this cshtml to "Copy to Output Directory" as "Copy Always" so that it appears in web project / bin/MyView/Index.cshtml.</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/OsOjB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OsOjB.png" alt="enter image description here"></a></p>
  
  <p>When I run the project, <a href="http://localhost" rel="nofollow noreferrer">http://localhost</a>:/MyView, it hits the "MyView" controller, but not finding the "MyView" view. </p>
  
  <blockquote>
    <p>Server Error in '/' Application.
    Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
    
    <p>Exception Details: System.FormatException: Index (zero based) must be
    greater than or equal to zero and less than the size of the argument
    list.</p>
    
    <p>Source Error: </p>
    
    <p>An unhandled exception was generated during the execution of the
    current web request. Information regarding the origin and location of
    the exception can be identified using the exception stack trace below.</p>
    
    <p>Stack Trace: </p>
    
    <p>[FormatException: Index (zero based) must be greater than or equal to
    zero and less than the size of the argument list.]<br>
    System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider,
    String format, ParamsArray args) +1876<br>
    System.String.FormatHelper(IFormatProvider provider, String format,
    ParamsArray args) +58    System.String.Format(IFormatProvider
    provider, String format, Object[] args) +80<br>
    System.Web.Mvc.ViewLocation.Format(String viewName, String
    controllerName, String areaName) +70<br>
    System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext
    controllerContext, List<code>1 locations, String name, String
    controllerName, String areaName, String cacheKey, String[]&amp;
    searchedLocations) +166<br>
    System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext
    controllerContext, String[] locations, String[] areaLocations, String
    locationsPropertyName, String name, String controllerName, String
    cacheKeyPrefix, Boolean useCache, String[]&amp; searchedLocations) +626<br>
    System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext
    controllerContext, String viewName, String masterName, Boolean
    useCache) +130<br>
    System.Web.Mvc.&lt;&gt;c__DisplayClass14_0.&lt;FindView&gt;b__1(IViewEngine e) +24
    System.Web.Mvc.ViewEngineCollection.Find(Func</code>2 lookup, Boolean
    trackSearchedPaths) +85<br>
    System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext
    controllerContext, String viewName, String masterName) +183<br>
    System.Web.Mvc.ViewResult.FindView(ControllerContext context) +84<br>
    System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
    +126    System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext
    controllerContext, ActionResult actionResult) +13<br>
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList<code>1
    filters, Int32 filterIndex, ResultExecutingContext preContext,
    ControllerContext controllerContext, ActionResult actionResult) +56<br>
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList</code>1
    filters, Int32 filterIndex, ResultExecutingContext preContext,
    ControllerContext controllerContext, ActionResult actionResult) +420<br>
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext
    controllerContext, IList<code>1 filters, ActionResult actionResult) +52<br>
    System.Web.Mvc.Async.&lt;&gt;c__DisplayClass3_6.&lt;BeginInvokeAction&gt;b__3()
    +198    System.Web.Mvc.Async.&lt;&gt;c__DisplayClass3_1.&lt;BeginInvokeAction&gt;b__5(IAsyncResult
    asyncResult) +100<br>
    System.Web.Mvc.Async.WrappedAsyncResult</code>1.CallEndDelegate(IAsyncResult
    asyncResult) +10<br>
    System.Web.Mvc.Async.WrappedAsyncResultBase<code>1.End() +49<br>
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult
    asyncResult) +27<br>
    System.Web.Mvc.&lt;&gt;c.&lt;BeginExecuteCore&gt;b__152_1(IAsyncResult
    asyncResult, ExecuteCoreState innerState) +11<br>
    System.Web.Mvc.Async.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult
    asyncResult) +29<br>
    System.Web.Mvc.Async.WrappedAsyncResultBase<code>1.End() +49<br>
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
    System.Web.Mvc.&lt;&gt;c.&lt;BeginExecute&gt;b__151_2(IAsyncResult asyncResult,
    Controller controller) +13<br>
    System.Web.Mvc.Async.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult
    asyncResult) +22<br>
    System.Web.Mvc.Async.WrappedAsyncResultBase<code>1.End() +49<br>
    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26<br>
    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult
    asyncResult) +10<br>
    System.Web.Mvc.&lt;&gt;c.&lt;BeginProcessRequest&gt;b__20_1(IAsyncResult
    asyncResult, ProcessRequestState innerState) +28<br>
    System.Web.Mvc.Async.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult
    asyncResult) +29<br>
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49<br>
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
    +28    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult
    result) +9<br>
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
    +583    System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +132    System.Web.HttpApplication.ExecuteStep(IExecutionStep
    step, Boolean&amp; completedSynchronously) +173</p>
  </blockquote>
  
  <p>Seems like I am missing something.</p>
  
  <p>not sure what ""~/bin/Views/{1}/{0}.cshtml" configurations doing and it seems failing in CustomViewEngine. </p>