ASP.NET MVC3剃须刀 - 从字符串创建视图? [英] ASP.NET MVC3 Razor - create view from a string?

查看:76
本文介绍了ASP.NET MVC3剃须刀 - 从字符串创建视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有从字符串返回而不是来自于磁盘上的文件的视图的便捷方式?

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

我已经实现了一个自定义的的VirtualPathProvider 处理检索从数据库的观点,但我并不总是希望查看存储在数据库中。

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.

更新2011年2月15日

我碰到一个,简化编制剃刀意见的过程中很不错的开源组件 code.I've取代大部分的虚拟路径提供code的这一部分,它的工作令人难以置信的好。我把它推荐给任何人的尝试编译从数据库视图或其他地方谁不需要一个虚拟路径提供的附加功能。此组件可让您在控制器/应用/任何(不需要网络环境和控制器上下文)范围内,而不必通过VPP跳火圈直接编译的看法。

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.

推荐答案

您可以通过创建自己运行视图中的 RazorTemplateEngine 其内容源,并编译成 WebViewPage

You can run the view yourself by creating a RazorTemplateEngine which reads your source and compiles into a 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的定制发电机方式:


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);
    }
}

这篇关于ASP.NET MVC3剃须刀 - 从字符串创建视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆