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();
}