我正在使用ServiceStack的Razor / Markdown引擎,并且在将自己的自定义模板/布局应用于某些渲染的Markdown时遇到了一些困难。 Markdown内容呈现完美,我只想将其注入我选择的模板/布局文件中。
目前我有这个(完美无缺):
var rootPath = HttpContext.Current.Server.MapPath("~/");
var markdownPath = Path.Combile(rootPath, "NotFound.md");
var format = new MarkdownFormat();
var markdownContent = File.ReadAllText(markdownPath);
const string pageTitle = "Not Found";
var page = new MarkdownPage(format, rootPath, pageTitle, markdownContent);
format.AddPage(page);
var scopeArgs = new Dictionary<string, object>();
var html = format.RenderDynamicPageHtml(pageTitle, scopeArgs);
现在我有一个我希望使用的布局文件位于“〜/ ErrorLayout.cshtml”,但是我不知道如何注入它。起初我想将MarkdownPage上的Template变量设置为我的Layout文件的路径,但是这不起作用。然后我尝试调用format.AddLayout(),不幸的是,它引发了异常。
任何帮助都将受到高度赞赏,如果我没有做出我想要做的事情,请随时向我自己寻求进一步的澄清。
答案 0 :(得分:0)
所以我已经解决了这个问题,但是我不确定我所做的是否是正确的方法,但是它有效并且不会抛出任何异常。也许有更多知识的人可以纠正我,如果我做错了(所以我会在接受我的答案之前将这个问题保持开放几天)。
我创建了一个实现IVirtualPathProvider接口的新类,并将其命名为PathProvider
我还创建了一个实现IVirtualFile接口的类,并将其命名为VirtualFile
然后我将MarkdownFormat实例中的VirtualPathProvider设置为PathProvider的新实例。然后我将我的Markdownpage实例上的Template变量设置为我想要使用的cshtml布局/模板文件的相对路径,并且在我之前提到的两个类中,在请求时返回此模板的相关内容。
我的代码现在看起来像这样(以防其他人遇到与我相同的问题):
var rootPath = HttpContext.Current.Server.MapPath("~/");
if (contents == null)
{
var notFoundPath = Path.Combine(rootPath, "NotFound.md");
contents = File.ReadAllText(notFoundPath);
}
var format = new MarkdownFormat
{
VirtualPathProvider = new PathProvider()
};
const string pageTitle = "Not Found";
var page = new MarkdownPage(format, rootPath, pageTitle, contents)
{
Template = "~/_Layout.cshtml"
};
format.AddPage(page);
var view = new Dictionary<string, object>();
var html = format.RenderDynamicPageHtml(pageTitle, view);
我的PathProvider类如下所示:
public class PathProvider : IVirtualPathProvider
{
public IVirtualDirectory RootDirectory { get; private set; }
public string VirtualPathSeparator { get; private set; }
public string RealPathSeparator { get; private set; }
public string CombineVirtualPath(string basePath, string relativePath)
{
throw new NotImplementedException();
}
public bool FileExists(string virtualPath)
{
throw new NotImplementedException();
}
public bool DirectoryExists(string virtualPath)
{
throw new NotImplementedException();
}
public IVirtualFile GetFile(string virtualPath)
{
return new VirtualFile(this, virtualPath);
}
public string GetFileHash(string virtualPath)
{
throw new NotImplementedException();
}
public string GetFileHash(IVirtualFile virtualFile)
{
throw new NotImplementedException();
}
public IVirtualDirectory GetDirectory(string virtualPath)
{
throw new NotImplementedException();
}
public IEnumerable<IVirtualFile> GetAllMatchingFiles(string globPattern, int maxDepth = 2147483647)
{
throw new NotImplementedException();
}
public bool IsSharedFile(IVirtualFile virtualFile)
{
throw new NotImplementedException();
}
public bool IsViewFile(IVirtualFile virtualFile)
{
throw new NotImplementedException();
}
}
最后是我的VirtualFile类:
public class VirtualFile : IVirtualFile
{
public IVirtualDirectory Directory { get; private set; }
public string Name { get; private set; }
public string VirtualPath { get; private set; }
public string RealPath { get; private set; }
public bool IsDirectory { get; private set; }
public DateTime LastModified { get; private set; }
public IVirtualPathProvider VirtualPathProvider { get; private set; }
public string Extension { get; private set; }
public VirtualFile(IVirtualPathProvider virtualPathProvider, string filePath)
{
VirtualPathProvider = virtualPathProvider;
VirtualPath = filePath;
RealPath = HttpContext.Current.Server.MapPath(filePath);
}
public string GetFileHash()
{
throw new NotImplementedException();
}
public Stream OpenRead()
{
throw new NotImplementedException();
}
public StreamReader OpenText()
{
throw new NotImplementedException();
}
public string ReadAllText()
{
return File.ReadAllText(RealPath);
}
}