是否有可能使用的Razor视图引擎外asp.net [英] Is it possible to use Razor View Engine outside asp.net

查看:105
本文介绍了是否有可能使用的Razor视图引擎外asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我看的Razor视图引擎,然后我看到一个非常漂亮和简洁的语法并不特别依赖于生成HTML。所以我想,这将是多么容易使用外asp.net发动机处于一个正常.NET环境,例如生成文本,code,...

If I look at the Razor View Engine, then I see a very nice and concise syntax that is not particularly tied to generating html. So I wonder, how easy would it be to use the engine outside asp.net in a "normal" .net environment for example to generate text, code,...

任何指针,例如,评论或解释是值得欢迎的。

Any pointer, example, comment or explanation is welcome.

推荐答案

有这里有两个问题:


  1. 是的,你可以在ASP.NET应用程序域的上下文之外运行的Razor视图引擎,在安德鲁的博客中解释:<一href=\"http://vibrant$c$c.com/blog/2010/11/16/hosting-razor-outside-of-aspnet-revised-for-mvc3-rc.html\" rel=\"nofollow\">http://vibrant$c$c.com/blog/2010/11/16/hosting-razor-outside-of-aspnet-revised-for-mvc3-rc.html

  2. 然而,剃刀仍然主要集中在该剃刀解析器使用&LT的presence感生成XML类标记(如HTML);标签&GT; 确定code和标记之间的过渡。你也许可以用它来生成任何文本,但你可能会遇到的问题时,你的输出不匹配对你的意图是什么Razor的假设。

  1. Yes, you can run the Razor View Engine outside of the context of an ASP.NET app domain, as explained in Andrew's blog: http://vibrantcode.com/blog/2010/11/16/hosting-razor-outside-of-aspnet-revised-for-mvc3-rc.html
  2. However, Razor is still primarily focused on generating xml-like markup (e.g. HTML) in the sense that the Razor parser uses the presence of <tags> to determine the transition between code and markup. You can probably use it to generate any text but you might run into issues when your output doesn't match Razor's assumptions about what your intentions are.

因此​​,举例来说,虽然这是有效的剃刀code(因为&LT; D​​IV&GT; 标记):

So for example while this is valid Razor code (because of the <div> tag):

@if(printHello) {
   <div>Hello!</div>
}

下面的代码片断是无效的(因为你好仍然被视为code!):

The following snippet is invalid (because the Hello! is still being treated as code):

@if(printHello) {
   Hello!
}

但是有一个特殊的&LT;文本&GT; 标签可以用来强制多行块(一过渡&LT;文本&GT; 标签将不会被渲染):

However there's a special <text> tag that can be used to force a transition for multi-line blocks (the <text> tag will not be rendered):

@if(printHello) {
   <text>Hello!
   Another line</text>
}

还有一个更短的语法来强制单行使用过渡 @

@if(printHello) {
   @:Hello!
}

这篇关于是否有可能使用的Razor视图引擎外asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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