我正在使用带有重写MasterLocationFormats的自定义剃刀视图引擎。我想为搜索视图和母版页提供相对虚拟路径位置。
怎么办呢?
public class ExampleRazorViewEngine : RazorViewEngine
{
/// <summary>
/// Initializes a new instance of the <see cref="ExampleRazorViewEngine"/> class.
/// </summary>
public ExampleRazorViewEngine()
{
ViewLocationFormats = new string[] {
"../../../Views/{1}/{0}.cshtml",
};
MasterLocationFormats = new string[] {
"../../../Views/{1}/{0}.cshtml",
};
PartialViewLocationFormats = new string[] {
"../../../Views/{1}/{0}.cshtml",
};
FileExtensions = new string[] { "cshtml" };
}
这样做会产生以下错误。 相对虚拟路径..&lt;路径&gt; ..这里不允许
答案 0 :(得分:0)
假设您要在项目目录中的插件文件夹中加载视图文件。那么你的代码就像下面的例子。
示例:强>
public class PluginViewEngine : RazorViewEngine
{
private List<string> _plugins = new List<string>();
public PluginViewEngine()
{
var plugins = Directory.GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins")).ToList();
//Load your directory
plugins.ForEach(s =>
{
var di = new DirectoryInfo(s);
_plugins.Add(di.Name);
});
ViewLocationFormats = GetViewLocations();
MasterLocationFormats = GetMasterLocations();
PartialViewLocationFormats = GetViewLocations();
}
public string[] GetViewLocations()
{
var views = new List<string> {
"~/Views/{1}/{0}.cshtml"
};
_plugins.ForEach(plugin =>
views.Add("~/Plugins/" + plugin + "/Views/{1}/{0}.cshtml")
); //Load your view
return views.ToArray();
}
public string[] GetMasterLocations()
{
var masterPages = new List<string> {
"~/Views/Shared/{0}.cshtml"
};
_plugins.ForEach(plugin =>
masterPages.Add("~/Plugins/" + plugin + "/Views/Shared/{0}.cshtml")
);//Load your view
return masterPages.ToArray();
}
}