在asp.net web项目剃须刀意见 [英] razor views in asp.net web project

查看:135
本文介绍了在asp.net web项目剃须刀意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究视图引擎和锐利的看法变得非常有趣的给我。我在开发一个asp.net 4.0 Web窗体应用程序的过程。剃刀欣赏到了我能找到的是predominantly与MVC应用的例子。

I am currently researching view engines and Razor Views have become very interesting to me. I am in process of developing an asp.net 4.0 web forms application. Razor views examples from what I could find are predominantly with MVC applications.

是否有可能剃刀意见整合到Web表单应用程序?它是有益的这样做呢?主要的原因我看这样做是为我的应用程序体系结构,并可能可以测试一个新的区域中创建一个新图层。

Is it possible to integrate Razor views in to a web forms application? Is it beneficial to do so? The main reason I look to do this is to create a new layer for my applications architecture and possibly a new area that can be tested.

推荐答案

当然可以!通过使用网页项目微软就可以加载剃须刀类以同样的方式,你通常会加载用户控件,通过给一个类/剃刀文件的路径。你获得了一个好康<实例/ A>,你可以执行,而且会给你你可以在页面上打印出的字符串。

Of course you can! By using the WebPages project from Microsoft you can load razor-classes the same way you would normally load an UserControl, by giving a path to a class/razor file. What you get back is an instance of a WebPage that you can execute and that will give you a string you can print out on your page.

我这个工作我自己,复合材料C1 CMS 实施剃刀功能和源$ C ​​$ C,因为这是可以免费从 HTTP://compositec1contrib.$c$cplex.com / 。我在这里强调的重要组成部分。

I've done this myself, implemented Razor functionality for Composite C1 CMS and the sourcecode for it is freely available from http://compositec1contrib.codeplex.com/. I'll highlight the important parts here.

请确保您有在web.config注册.cshtml文件的生成提供

Make sure you have a build provider for .cshtml files registered in the web.config

请确保您有必要的system.web.webPages.razor配置设置

Make sure you have the necessary system.web.webPages.razor configuration setup

实例化这样的 VAR网页= WebPage.CreateInstanceFromVirtualPath(_relativeFilePath)一.cshtml文件的一个实例; (<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.webpages.webpagebase.createinstancefromvirtualpath%28v=vs.99%29.aspx\"相对=nofollow>看DOC )

获取剃刀类这样的输出

var httpContext = new HttpContextWrapper(HttpContext.Current);
var pageContext = new WebPageContext(httpContext, webPage, null);

var sb = new StringBuilder();
using (var writer = new StringWriter(sb))
{
   webPage.ExecutePageHierarchy(pageContext, writer);
}

string output = sb.ToString();

只是输出您的WebForms字符串页

Just output the string on your WebForms Page

这篇关于在asp.net web项目剃须刀意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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