Nancy - 视图引擎

The concept of a view engine is quite simple - it takes a “template” and an optional “model” (the data) and outputs (usually) HTML to be rendered into the browser. By default, Nancy ships with a built in view engine known as the SuperSimpleViewEngine, which supports all the necessities such as layouts, partials, models, conditions and iterations. You can use this without any additional dependencies and it serves up .html and .sshtml files.

The following is an example of what a .sshtml file can look like

@Master[‘MasterPage‘]

@Section[‘Content‘]
    <p>This content from the index page<p>
    <h3>Partials</h3>
    <p>Login box below rendered via a partial view with no model.</p>
    <div id="login">
        @Partial[‘login‘];
    </div>
    <p>Box below is rendered via a partial with a sub-model passed in.</p>
    <p>The submodel is a list which the partial iterates over with Each</p>
    <div id="users">
        @Partial[‘user‘, Model.Users];
    </div>
    <h3>Encoding</h3>
    <p>Model output can also be encoded:</p>
    <p>@!Model.NaughtyStuff</p>
@EndSection

In addition to the built in engine, Nancy also supports Razor, Spark, NDjango and the dotLiquid view engines as separate packages. By adding a reference to them from your project Nancy will automatically be able to render views using their supported file extensions.

Rendering a view from a route action

To render a view you simply use the View indexer of the NancyModule with the appropriate parameters, which might sound complicated, but it really isn’t... honest! :-). There are three overloads which allow you to pass a view name, a model, or both and Nancy will take care of the rest for you, locating the view using a series of “conventions”, which we will discuss later:

Get["/products"] = parameters => {
    return View["products.html", someModel];
};

The view name offers you quite a bit of flexibility in terms of what information it can contain:

  1. Name of the view file with the file extension present (e.g. “products.html”)
  2. Name of the view file without the file extension present (e.g. “products”). If there are two, or more, views with the same name, but with different extensions, you will receive anAmbiguousViewsException with details on which conflicting views were located.
  3. A path, relative to the application root, where Nancy will look for the views (e.g.products/products.html).

To learn more on where Nancy looks for views, and how to customize the logic, check out View location conventions

Resolving the name of the view from the model

If you only pass in a model to the View indexer Nancy will attempt to infer the name and location of the view using another of the default conventions.

The convention strips “Model” off the end of the model type if it exists (e.g. ProductsModel becomes products), and then uses that name to attempt to locate the view.

In the following route, Nancy will try to locate a view called Products with a file extension that matches any of the installed view engines:

Get["/products"] = parameters => {
    return View[new ProductsModel()];
};

As part of the new Content Negotiation features in 0.12, if Nancy could not find your view, the server will return 406 Not Acceptable instead.

时间: 2024-11-13 16:06:00

Nancy - 视图引擎的相关文章

NancyFX 第六章 视图引擎

正如其他的Web工具包,Nancy也有视图的概念,用来描述在浏览器上看到的输出 视图的定义 你可能没有之前没有接触过"视图"的概念,或是仅仅是从其他工具包例如ASP.NET MVC 中听说过.(其中MVC中"V"代表"View") 你是否想过视图到底是什么那? 根据牛津字典的释义,视图时能够看到的东西,或是在特定情况下看到的东西. 视图是终端用户在浏览器中看到的东西.根据定义浏览器中可以看到的输出都可以称之为视图. 至于我们在次的定义的视图应该是

MVC视图引擎

1.视图引擎:把视图解析成浏览器可执行的html代码 2.aspx视图: <%=表达式%>: <% C#代码段 %>: 3.razor视图: @(表达式):@ViewData["name"],如果@后跟常量,必须用括号括起来:@(“hello”) @{C#代码段}:@{ if(a>b) { return true; } } 引入命名空间:@using System.IO 双@符号转义,输出@:<span>[email protected]@qq

ASP.Net MVC开发基础学习笔记(3):Razor视图引擎、控制器与路由机制学习

首页 头条 文章 频道                         设计频道 Web前端 Python开发 Java技术 Android应用 iOS应用 资源 小组 相亲 频道 首页 头条 文章 小组 相亲 资源 设计 前端 Python Java 安卓 iOS 登录 注册 首页 最新文章 经典回顾 开发 Web前端 Python Android iOS Java C/C++ PHP .NET Ruby Go 设计 UI设计 网页设计 交互设计 用户体验 设计教程 设计职场 极客 IT技术

Razor 视图引擎 &ndash; ASP.NET MVC 4 系列

       Razor 视图引擎是 ASP.NET MVC 3 开始扩展的内容,并且也是默认视图引擎.        Razor 通过理解标记的结构来实现代码和标记之间尽可能顺畅的转换.下面的例子演示了一个包含少量视图逻辑的简单 Razor 视图: @{ // this is a block of code. For demonstration purposes, // we'll create a "model" inline. var items = new string[] {

ASP.NET Razor 视图引擎编程参考

ASP.NET Razor 视图引擎编程参考 转载请注明出处:http://surfsky.cnblogs.com Rasor 视图引擎    http://msdn.microsoft.com/zh-cn/library/ff849693.aspx    http://www.microsoft.com/downloads/en/details.aspx?FamilyID=b7937c34-3b53-47b7-ae17-5a72fa700472&displaylang=en    http:/

ASP.NET的另一个视图引擎——Razor

 一.什么是Razor? 要是你知道什么是ASPX,那我就告诉你:Razor是和ASPX 一样的另一种视图引擎.说到视图就明白了很多吧.既然都为视图引擎,都是MS的东西,两个视图引擎,一前一后,肯定有区别.那么接下来让我们来认识一下Razor吧. 首先,Razor是作为一种后起的视图模板被ASP.NET MVC3使用. 这就暴露了Razor的一个重要信息:界面设计灵活,比aspx视图引擎更专注于WEB前端页面的设计.因为MVC就是基于WEB开发所诞生的一个设计模式,主旨之一就是将页面和业务逻辑解

MVC Razor视图引擎

Razor 不是编程语言.它是服务器端标记语言. Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法 当网页被写入浏览器时,基于服务器的代码能够创建动态内容.在网页加载时,服务器在向浏览器返回页面之前,会执行页面内的基于服务器代码.由于是在服务器上运行,这种代码能执行复杂的任务,比如访问数据库. Razor在减少代码冗余.增强代码可读性和VS智能感知方面,都有着突出的优势. Razor一经推出就深受所有ASP.Net开发者的喜爱. Razor 使

ASP.NET MVC 对于视图引擎的优化

我经常使用asp.net MVC框架来做网站.总的来说,MVC框架是一个非常优秀的框架.相对于曾经的web form模式,我个人感觉分工更加合理思路也更加清晰,但是交给开发人员的工作也相对变多了. 当使用标准配置的时候在新建了控制器,还没有建视图的时候,运行网站,访问这个我们可以看到 因此我们可以判断,默认的视图引擎首先加载的顺序如上图所示 ~/Views/Home/Index.aspx~/Views/Home/Index.ascx~/Views/Shared/Index.aspx~/Views

ASP.NET MVC3 系列教程 - Razor视图引擎基础语法

http://www.cnblogs.com/highend/archive/2011/04/09/aspnet_mvc3_razor_engine.html 4. 关于所有带"_"开头的文档 ASP.NET MVC3默认情况下会拒绝所有访问地址以"_"开头的_*.cshtml文档.关于_*.cshtml文档,其实他是WebPages中的一部分,稍后将会详细介绍该以"_"文档的使用说明. 例如访问 http://localhost:7985/_V