1

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変数をレイアウトファイルのパスに設定しようと思いましたが、うまくいきませんでした。次に、format.AddLayout()を呼び出そうとしましたが、残念ながら例外がスローされました。

何か助けていただければ幸いです。私がやろうとしていることを明確にしていない場合は、遠慮なく私自身にさらに説明を求めてください。

4

1 に答える 1

0

だから私は問題を解決しました、しかし私がしたことが正しい方法であるかどうかはわかりません、しかしそれはうまくいき、例外はスローされません。私がこれを間違って行った場合、おそらくもう少し知識のある人が私を訂正する可能性があります(したがって、私の答えを受け入れる前に、この質問を数日間開いたままにしておきます)。

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);
    }
}
于 2012-12-17T19:40:54.087 に答える