1

I was following this Mohsen Esmailpour's answer, to build a sample project to load views and controllers from a class library using custom view engine.

Here's what I did,

  1. Add a Asp.Net MVC project
  2. Add a class library. Add references to
    • Microsoft.AspNet.Razor
    • Microsoft.AspNet.WebPages
    • Microsoft.Web.Infrastructure
    • Microsoft.AspNet.Mvc"

Reference class library in MVC project. 3. Add a "Controllers" folder and added "MyViewController.cs" class

public class MyViewController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

View engine (which should be causing the problem), I am not entirely sure what's happening here,

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"
        };
    }
}

Route.config for the Mvc project,

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 });
    }
}
  1. 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.

enter image description here

When I run the project, http://localhost:/MyView, it hits the "MyView" controller, but not finding the "MyView" view.

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.

Exception Details: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Source Error:

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.

Stack Trace:

[FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.]
System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args) +1876
System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args) +58 System.String.Format(IFormatProvider provider, String format, Object[] args) +80
System.Web.Mvc.ViewLocation.Format(String viewName, String controllerName, String areaName) +70
System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext controllerContext, List1 locations, String name, String controllerName, String areaName, String cacheKey, String[]& searchedLocations) +166
System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations) +626
System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache) +130
System.Web.Mvc.<>c__DisplayClass14_0.<FindView>b__1(IViewEngine e) +24 System.Web.Mvc.ViewEngineCollection.Find(Func
2 lookup, Boolean trackSearchedPaths) +85
System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext controllerContext, String viewName, String masterName) +183
System.Web.Mvc.ViewResult.FindView(ControllerContext context) +84
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +126 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +52
System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__3() +198 System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__5(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45 System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +583 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +132 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +173

Seems like I am missing something.

not sure what ""~/bin/Views/{1}/{0}.cshtml" configurations doing and it seems failing in CustomViewEngine.

Dhanuka777
  • 8,331
  • 7
  • 70
  • 126
  • Solved this one by myself. Issue was ViewLocationFormats configuration not correct in CustomViewEngine which I corrected as "~/bin/Views/{1}/{0}.cshtml" and started working. After doing that I noticed I have to include "@inherits System.Web.Mvc.WebViewPage" in the Index.cshtml and it loaded my custom view. Hooray! – Dhanuka777 Feb 17 '18 at 14:16
  • It would be great if you could post the solution as an answer. – NightOwl888 Feb 18 '18 at 08:08
  • @Dhanuka777 - could you post your full solution to this - I'm getting the same error trying to use a custom RazorViewEngine, thanks. – Matt Apr 11 '19 at 14:21

1 Answers1

0

I had a typo in the folder's name, the folder in which my view was located.

Alkemichar
  • 81
  • 1
  • 3