ASP.NET MVC3 Razor - 从字符串创建视图?

发布于 2024-10-15 22:11:03 字数 415 浏览 1 评论 0 原文

有没有一种方便的方法可以从字符串返回视图,而不必从磁盘上的文件返回视图?

我已经实现了一个自定义 VirtualPathProvider 来处理从数据库检索视图,但我并不总是希望将视图存储在数据库中。

更新 2-15-2011

我偶然发现了一个非常好的开源组件,它简化了在代码中编译 Razor 视图的过程。我已经用这个组件替换了大部分虚拟路径提供程序代码,并且它运行得非常好。我将其推荐给任何尝试从数据库或其他地方编译视图且不需要虚拟路径提供程序附加功能的人。该组件允许您直接在控制器/应用程序/任何内容(不需要 Web 上下文和控制器上下文)中编译视图,而无需跳过 VPP 环。

Is there a convenient way to return a view from a string instead of having to come from a file on disk?

I've implemented a custom VirtualPathProvider that handles retrieving views from a database, but I don't always want the view to be stored in the database.

Update 2-15-2011

I stumbled across a very nice open source component that simplifies the process of compiling Razor views in code.I've replaced much of the Virtual Path Provider code with this component, and it's working incredibly well. I recommend it to anyone that's trying to compile views from a database or elsewhere who doesn't need the additional capabilities of a virtual path provider. This component lets you compile the view directly within your controller/app/whatever (web context and controller context not required) without having to jump through the VPP hoops.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2024-10-22 22:11:03

您可以通过创建一个 RazorTemplateEngine 来自行运行该视图,该引擎读取您的源代码并编译为 WebViewPage

WebViewPage

webViewPage.OverridenLayoutPath = LayoutPath;
webViewPage.VirtualPath = ViewPath;
webViewPage.ViewContext = viewContext;
webViewPage.ViewData = viewContext.ViewData;

webViewPage.InitHelpers();
WebPageRenderingBase startPage = null;
if (RunViewStartPages) {
    startPage = StartPageLookup(webViewPage, RazorViewEngine.ViewStartFileName, ViewStartFileExtensions);
}
webViewPage.ExecutePageHierarchy(new WebPageContext(context: viewContext.HttpContext, page: null, model: null), writer, startPage);

然后,您可以通过写入支持新的 @model 关键字,您需要重写 RazorEngineHost 中的方法才能使用 MVC 的自定义生成器:

public override RazorCodeGenerator DecorateCodeGenerator(RazorCodeGenerator incomingCodeGenerator) {
    if (incomingCodeGenerator is CSharpRazorCodeGenerator) {
        return new MvcCSharpRazorCodeGenerator(incomingCodeGenerator.ClassName,
                                               incomingCodeGenerator.RootNamespaceName,
                                               incomingCodeGenerator.SourceFileName,
                                               incomingCodeGenerator.Host);
    }
    else if (incomingCodeGenerator is VBRazorCodeGenerator) {
        return new MvcVBRazorCodeGenerator(incomingCodeGenerator.ClassName,
                                           incomingCodeGenerator.RootNamespaceName,
                                           incomingCodeGenerator.SourceFileName,
                                           incomingCodeGenerator.Host);
    }
    return base.DecorateCodeGenerator(incomingCodeGenerator);
}

public override ParserBase DecorateCodeParser(ParserBase incomingCodeParser) {
    if (incomingCodeParser is CSharpCodeParser) {
        return new MvcCSharpRazorCodeParser();
    }
    else if (incomingCodeParser is VBCodeParser) {
        return new MvcVBRazorCodeParser();
    }
    else {
        return base.DecorateCodeParser(incomingCodeParser);
    }
}

You can run the view yourself by creating a RazorTemplateEngine which reads your source and compiles into a WebViewPage.

You can then run the WebViewPage by writing

webViewPage.OverridenLayoutPath = LayoutPath;
webViewPage.VirtualPath = ViewPath;
webViewPage.ViewContext = viewContext;
webViewPage.ViewData = viewContext.ViewData;

webViewPage.InitHelpers();
WebPageRenderingBase startPage = null;
if (RunViewStartPages) {
    startPage = StartPageLookup(webViewPage, RazorViewEngine.ViewStartFileName, ViewStartFileExtensions);
}
webViewPage.ExecutePageHierarchy(new WebPageContext(context: viewContext.HttpContext, page: null, model: null), writer, startPage);

To support the new @model keyword, you'll need to override methods in your RazorEngineHost to use MVC's custom generators:

public override RazorCodeGenerator DecorateCodeGenerator(RazorCodeGenerator incomingCodeGenerator) {
    if (incomingCodeGenerator is CSharpRazorCodeGenerator) {
        return new MvcCSharpRazorCodeGenerator(incomingCodeGenerator.ClassName,
                                               incomingCodeGenerator.RootNamespaceName,
                                               incomingCodeGenerator.SourceFileName,
                                               incomingCodeGenerator.Host);
    }
    else if (incomingCodeGenerator is VBRazorCodeGenerator) {
        return new MvcVBRazorCodeGenerator(incomingCodeGenerator.ClassName,
                                           incomingCodeGenerator.RootNamespaceName,
                                           incomingCodeGenerator.SourceFileName,
                                           incomingCodeGenerator.Host);
    }
    return base.DecorateCodeGenerator(incomingCodeGenerator);
}

public override ParserBase DecorateCodeParser(ParserBase incomingCodeParser) {
    if (incomingCodeParser is CSharpCodeParser) {
        return new MvcCSharpRazorCodeParser();
    }
    else if (incomingCodeParser is VBCodeParser) {
        return new MvcVBRazorCodeParser();
    }
    else {
        return base.DecorateCodeParser(incomingCodeParser);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文