だから私は問題を解決しました、しかし私がしたことが正しい方法であるかどうかはわかりません、しかしそれはうまくいき、例外はスローされません。私がこれを間違って行った場合、おそらくもう少し知識のある人が私を訂正する可能性があります(したがって、私の答えを受け入れる前に、この質問を数日間開いたままにしておきます)。
IVirtualPathProviderインターフェイスを実装する新しいクラスを作成し、PathProviderという名前を付けました。
また、IVirtualFileインターフェイスを実装するクラスを作成し、VirtualFileという名前を付けました。
次に、MarkdownFormatのインスタンスのVirtualPathProviderをPathProviderの新しいインスタンスに設定します。次に、MarkdownpageのインスタンスのTemplate変数を、使用したいcshtmlレイアウト/テンプレートファイルへの相対パスに設定し、前述の2つのクラス内で、要求されたときにこのテンプレートの関連コンテンツを返しました。
私のコードは次のようになります(他の誰かが私と同じ問題を抱えている場合に備えて):
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);
}
}