我已經在這篇文章的底部實現了mcfea答案,但它不起作用。MVC 4查看引擎搜索位置
Adding sub-directory to "View/Shared" folder in ASP.Net MVC and calling the view
我有下查看的子目錄/共享稱爲時間表。
這是例外。
未找到'時間表'或其主人的視圖或沒有查看引擎 支持搜索的位置。以下位置是 搜索:〜/ Views/Home/Timesheet.aspx〜/ Views/Home/Timesheet.ascx 〜/ Views/Shared/Timesheet.aspx〜/ Views/Shared/Timesheet.ascx 〜/ Views/Home /Timesheet.cshtml〜/查看/主頁/ Timesheet.vbhtml 〜/查看/共享/ Timesheet.cshtml〜/查看/共享/ Timesheet.vbhtml
馬爾科姆
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();
}
編輯2:即使這不工作
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();
}
感謝大衛這是問題。 – Malcolm 2014-10-08 03:17:51