2012-12-15 31 views
1

我使用ServiceStack的Razor/Markdown引擎,並且在將某些渲染的Markdown應用到我自己的自定義模板/佈局時遇到了一些困難。 Markdown內容呈現完美,我只想將其注入到我選擇的模板/佈局文件中。如何以編程方式爲使用ServiceStack Markdown呈現的HTML設置模板?

目前我有這個(工作完美):

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(),不幸的是拋出了一個異常。

任何幫助將受到高度讚賞,隨時要求從我自己進一步的澄清,如果我沒有做出我想要明確的。

回答

0

所以我解決了這個問題,但是我不確定我所做的是否是正確的方法,但是它的工作原理並沒有引發任何異常。如果我錯誤地做了這件事,也許有人有更多的知識可以糾正我(所以我會在接受我的答案之前將這個問題開放幾天)。

我創建了實現IVirtualPathProvider接口的新類,並把它命名爲PathProvider

我也創建它實現了IVirtualFile接口的類,並將其命名爲VirtualFile

然後我設置的VirtualPathProvider在我的MarkdownFormat實例添加到PathProvider的新實例。然後,將我的Markdownpage實例上的Template變量設置爲我想要使用的cshtml佈局/模板文件的相對路徑,並在之前提到的兩個類中爲此Template返回相關內容。

我的代碼現在看起來像這樣(如果別人有同樣的問題,因爲我):

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); 
    } 
} 
相關問題