我已经在这篇文章的底部实现了mcfea答案,但它没有用。
Adding sub-directory to "View/Shared" folder in ASP.Net MVC and calling the view
我在Views / Shared下有一个名为Timesheet的子文件夹。
这是例外。
未找到视图'Timesheet'或其主页或没有视图引擎 支持搜索的位置。以下地点是 搜索:〜/ 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
马尔科姆
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();
}
答案 0 :(得分:2)
您链接的答案是专门返回部分视图,而不是主视图。所以里面的代码说的是这样的:
string[] NewPartialViewFormats =
{
"~/Views/{1}/Timesheet/{0}.cshtml",
"~/Views/Shared/Timesheet/{0}.cshtml"
};
base.PartialViewLocationFormats =
base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
请注意,它使用PartialViewLocationFormats属性。如果要对所有视图使用此方法,还应将格式添加到ViewLocationFormats属性中:
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();
答案 1 :(得分:0)
如果你在子文件夹下传递比需要传递完整的cshtml页面路径。如果您不想提供完整路径而不是共享文件夹下的简单添加页面。