2
votes

I have implemented mcfea answer on the bottom of this post but it is not working.

Adding sub-directory to "View/Shared" folder in ASP.Net MVC and calling the view

I have a subfolder under Views/Shared called Timesheet.

This the exception.

The view 'Timesheet' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Home/Timesheet.aspx ~/Views/Home/Timesheet.ascx ~/Views/Shared/Timesheet.aspx ~/Views/Shared/Timesheet.ascx ~/Views/Home/Timesheet.cshtml ~/Views/Home/Timesheet.vbhtml ~/Views/Shared/Timesheet.cshtml ~/Views/Shared/Timesheet.vbhtml

Malcolm

public class MylesterViewEngine : RazorViewEngine
{
    private static readonly string[] NewPartialViewFormats =
{
    "~/Views/{1}/Timesheet/{0}.cshtml",
    "~/Views/Shared/Timesheet/{0}.cshtml"
};

    private static List<string> AreaRegistrations;

    public MylesterViewEngine()
    {
        AreaRegistrations = new List<string>();

        BuildAreaRegistrations();

        base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
        base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(AreaRegistrations).ToArray();
    }

    private static void BuildAreaRegistrations()
    {
        string[] areaNames = RouteTable.Routes.OfType<Route>()
            .Where(d => d.DataTokens != null && d.DataTokens.ContainsKey("area"))
            .Select(r => r.DataTokens["area"].ToString()).ToArray();

        foreach (string areaName in areaNames)
        {
            AreaRegistrations.Add("~/Areas/" + areaName + "/Views/Shared/Timesheet/{0}.cshtml");
            AreaRegistrations.Add("~/Areas/" + areaName + "/Views/{1}/Timesheet/{0}.cshtml");
        }
    }
}

  protected void Application_Start()
    {
        //AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();


        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new MylesterViewEngine());

        //var unityContainer = ModelContainer.Instance;
        //DependencyResolver.SetResolver(new UnityDependencyResolver(unityContainer));
        Bootstrapper.Initialise();

    }

EDIT 2: Even this doesnt work

   protected void Application_Start()
    {
        //AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();

        RazorViewEngine razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().FirstOrDefault();
        if (razorEngine != null)
        {
            string[] newPartialViewFormats = new[] { 
                    "~/Views/{1}/Timesheet/{0}.cshtml",
                    "~/Views/Shared/Timesheet/{0}.cshtml"
            };
            razorEngine.PartialViewLocationFormats =
                razorEngine.PartialViewLocationFormats.Union(newPartialViewFormats).ToArray();
        }

        //ViewEngines.Engines.Clear();
        //ViewEngines.Engines.Add(new MylesterViewEngine());

        //var unityContainer = ModelContainer.Instance;
        //DependencyResolver.SetResolver(new UnityDependencyResolver(unityContainer));
        Bootstrapper.Initialise();

    }
2

2 Answers

2
votes

The answer you linked to was for specifically returning partial views, not main views. So the code inside it says something like this:

string[] NewPartialViewFormats =
{
    "~/Views/{1}/Timesheet/{0}.cshtml",
    "~/Views/Shared/Timesheet/{0}.cshtml"
};

base.PartialViewLocationFormats = 
    base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();

Notice that it uses the PartialViewLocationFormats property. If you want to use this method for all views, you should also add your formats to the ViewLocationFormats property:

string[] NewPartialViewFormats =
{
    "~/Views/{1}/Timesheet/{0}.cshtml",
    "~/Views/Shared/Timesheet/{0}.cshtml"
};

//Add to partial views
base.PartialViewLocationFormats = 
    base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();

//Add to main views
base.ViewLocationFormats = 
    base.ViewLocationFormats.Union(NewPartialViewFormats).ToArray();
0
votes

If you passing under subfolder than need to pass full cshtml page path. If you don't want provide full path than simple add page under Shared folder.