且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有可能使用的Razor视图引擎外asp.net

更新时间:2023-01-14 16:57:59

有这里有两个问题:


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

  2. 然而,剃刀仍然主要集中在该剃刀解析器使用&LT的presence感生成XML类标记(如HTML);标签> 确定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!
}