日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Nancy 学习-视图引擎 继续跨平台

發布時間:2024/4/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nancy 学习-视图引擎 继续跨平台 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前面一篇,講解Nancy的基礎,以及Nancy自宿主,現在開始學習視圖引擎。

Nancy 目前支持兩種 一個是SSVE 一個是Razor。下面我們一起學習。

The Super Simple View Engine

SSVE 全稱就是?Super Simple View Engine ,翻譯過來也就是?超級簡單視圖引擎

我們在Nancy 不需單獨引用,因為它是默認內置的。該引擎處理任何sshtml,html或htm文件

模型可以是標準的類型,或ExpandoObject(或者實現 IDynamicMetaObjectProvider ,或者實現的IDictionary<string,object> 以訪問其屬性)。

SSVE是一個基于正則表達式的視圖引擎,沒有“執行代碼”,所以你不能指定自己的代碼來執行。內置的語法/命令,你可以使用如下所示。

?

Standard variable substitution

Replaces with the string representation of the parameter, or the model itself if a parameter is not specified. If the substitution can not be performed, for instance if you specify an invalid model property, it will be substituted with?[Err!]

Syntax

@Model[.Parameters]

Example

Hello @Model.Name, your age is @Model.User.Age

Iterators

Enables you to iterate over models that are collection. Iterators cannot be nested

Syntax

@Each[.Parameters][@Current[.Parameters]] @EndEach

@Each?will implicitly be associated with the model and for each iteration the?@Current?will represent the current item in the collection.?@Current?can be used multiple times in the iterator block, and is accessed in the same way as?@Model.

Example

@Each.UsersHello @Current.Name! @EndEach

Conditionals

Parameters must be a boolean (see Implicit Conditionals below). Nesting of @If and @IfNot statements is not supported.

Syntax:

@If[Not].Parameters[contents] @EndIf

Example

@IfNot.HasUsersNo users found! @EndIf

Implicit Conditionals

If the model has property that implements?ICollection?then you can use an implicit conditional. The implicit conditional syntax is the same as a normal conditional, but the?Parameters?part can have a?Has-prefix. The conditional will be true if the collection contains items, and false if it does not or if it is null.

Syntax

Has[CollectionPropertyName]

Example

@If.HasUsersUsers found! @EndIf

The above example will expand to "Users found!" if the model has a collection called?Users?and it contains items; if the collection is empty then the text would not be displayed.

HTML Encoding

Both the?@Model?and?@Current keywords?(with or without parameters) can have an optional?!operator, after the?@, to HTML encode the output.

Syntax

@!Model[.Parameter] @!Current[.Parameter]

Example

@!Model.Test@Each@!Current.Test @EndEach

Partials

Renders a partial view. A property of the current model can be specified to be used as the partial view's model, or it may be omitted to use the current view's model instead. The file extension of the view is optional.

Syntax

@Partial['<view name>'[, Model.Property]]

Example

// Renders the partial view with the same model as the parent @Partial['subview.sshtml'];// Renders the partial view using the User as the model @Partial['subview.sshtml', Model.User];

Master pages and sections

You can put shared layout in a master page and declare content sections that will be populated by the views. It is possible to have nested master pages and you are not obligated to provide content for all of the content sections.

The master pages will have access to the?@Model?of the view and the file extension is optional when specifying the name of the master to use in your view.

You can use the?@Section?tag multiple times and is used to both declare a content section, in a master page, and to define the content blocks of a view.

Syntax

@Master['<name>']@Section['<name>'] @EndSection

Example

// master.sshtml <html> <body> @Section['Content']; </body> </html>// index.sshtml @Master['master.sshtml']@Section['Content']This is content on the index page @EndSection

Anti-forgery token

Renders an anti-forgery token, on the page, in an hidden input to prevent cross-site request forgery attacks. The token will automatically be validated when a new request is posted to the server (assuming CSRF protection hasn’t been turned off).

Syntax

@AntiForgeryToken

Example

@AntiForgeryToken

Path expansion

Expands a relative paths to a fully qualified URL.

Syntax

@Path['<relative-path>']

Example

@Path['~/relative/url/image.png']

Starting from v1.2, SSVE performs automatic path expansion in all HTML attributes (more specifically, in all?name="value"?pairs, both with single and double quotes around?value) where attribute value starts with?~/. For example,?<a href="@Path['~/relative/path']" ...>?can be significantly shortened to?<a href="~/relative/path" ...>.

?

下面來講解一些常用的命令。

1.Model

2.Each

3.If

4.Partials

5.Master pages and sections

首先創建一個項目。建議創建web空項目 。

我是直接使用上次的項目?http://www.cnblogs.com/linezero/p/5121887.html

先創建一個Module ?SSVEModule

然后添加Views文件夾 -》然后再在其下添加 SSVE文件夾 -》添加對應的View 頁。

這樣使項目更加清楚。

1.Model

Get["/model"] = r =>{var model = "我是字符串";return View["model", model];};

在SSVE 文件夾下添加一個model.html?

<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head><meta charset="utf-8" /><title></title> </head> <body>@Model </body> </html>

然后我們訪問頁面?訪問地址:?http://localhost:9000/ssve/model

2.Each

Get["/each"] = r =>{var arr = new int[] { 3, 6, 9, 12, 15, 12, 9, 6, 3 };return View["each", arr];};

?each.html

<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head><meta charset="utf-8" /><title></title> </head> <body>@Each<p>@Current</p>@EndEach </body> </html>

訪問地址:?http://localhost:9000/ssve/each?

?

3.If

Get["/if"] = r =>{return View["if", new { HasModel = true }];};

if.html


<!
DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head><meta charset="utf-8" /><title></title> </head> <body>@If.HasModel<p>我出現了</p>@EndIf@IfNot.HasModel<p>我沒辦法出現</p>@EndIf </body> </html>

訪問地址:?http://localhost:9000/ssve/if

?

4.Partials

@Partial['header.html']

在SSVE 下添加header.html ?然后在頁面添加這句即可。

?

5.Master pages and sections

master.html

<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head><meta charset="utf-8" /><title></title> </head> <body>@Partial['header.html']@Section['Content'] </body> </html>

使用 master

@Master['master.html'] @Section['Content'] <p>master partial content 結合</p>@Model @EndSection

SSVEModule.cs

using Nancy; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace NancyDemo {public class SSVEModule : NancyModule{public SSVEModule():base("/ssve") {Get["/"] = r =>{var os = System.Environment.OSVersion;return "Hello SSVE<br/> System:" + os.VersionString;};Get["/model"] = r =>{var model = "我是字符串";return View["model", model];};Get["/each"] = r =>{var arr = new int[] { 3, 6, 9, 12, 15, 12, 9, 6, 3 };return View["each", arr];};Get["/if"] = r =>{return View["if", new { HasModel = true }];};}} }

?

SSVE視圖引擎源碼:https://github.com/grumpydev/SuperSimpleViewEngine

?

Razor View Engine

Razor 相信大家都是非常熟悉,所以也就不在這里過多做語法講解。

主要是講解在Nancy中使用Razor 視圖引擎。

Nancy 的Razor 是自定義實現的,所以與ASP.NET MVC 中的Razor 有所不同。

在Nancy中綁定模型是@Model ?不是ASP.NET MVC @model

安裝

要在Nancy中使用Razor 需要安裝?Nancy.ViewEngines.Razor

Nuget:Install-Package Nancy.Viewengines.Razor

添加Razor以后,會默認在app.config 添加Razor相關配置。

使用

建議大家新建一個空的web項目,這樣便于編寫視圖。

在視圖中聲明 關鍵字為:@inherits

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>

其他語法與ASP.NET MVC Razor相同。

我還是在原項目上進行添加。

先創建一個Module ?RazorModule

然后添加Views文件夾 -》然后再在其下添加 Razor文件夾 -》添加對應的View 頁。以 cshtml結尾的文件,也就是視圖文件。

1.Model

Get["/index"] = r =>{var model = "我是 Razor 引擎";return View["index",model];};

?

index.cshtml

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic> <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head><meta charset="utf-8" /><title></title> </head> <body>@Model </body> </html>

訪問地址:?http://localhost:9000/razor/index

2.each

Get["/each"] = r =>{var arr = new int[] { 3, 6, 9, 12, 15, 12, 9, 6, 3 };return View["each", arr];};

雖然Module中的代碼與前面相同。但View 就不一樣了。

each.cshtml

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic> <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head><meta charset="utf-8" /><title></title> </head> <body>@foreach (var item in Model){<p>@item</p>} </body> </html>

訪問地址:?http://localhost:9000/razor/each

RazorModule.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Nancy;namespace NancyDemo {public class RazorModule:NancyModule{public RazorModule() :base("/razor"){Get["/"] = r =>{var os = System.Environment.OSVersion;return "Hello Razor<br/> System:" + os.VersionString;};Get["/index"] = r =>{var model = "我是 Razor 引擎";return View["index",model];};Get["/each"] = r =>{var arr = new int[] { 3, 6, 9, 12, 15, 12, 9, 6, 3 };return View["each", arr];};}} }

?

項目結構

?

因為我使用的項目是控制臺程序,Views 文件夾下的文件必須都要在 屬性 選擇?始終復制

在linux上運行可以參考上篇文章。

最后留個坑,下一篇:Nancy 學習-進階部分?繼續跨平臺。請大家多多支持。

?

?參考鏈接:

https://github.com/NancyFx/Nancy/wiki/The-Super-Simple-View-Engine

?

如果你覺得本文對你有幫助,請點擊“推薦”,謝謝。

?

轉載于:https://www.cnblogs.com/linezero/p/5151305.html

總結

以上是生活随笔為你收集整理的Nancy 学习-视图引擎 继续跨平台的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 黄色一极片 | 日韩色影院 | 狠狠综合一区 | 欧美激情影院 | 91插插插插 | 国产午夜视频在线播放 | av中文网 | 91超碰在线播放 | 在线午夜电影 | 欧美视频在线观看一区 | 久久久久久黄色片 | 今天高清视频在线观看视频 | 国产黄色大片网站 | 国产污污视频在线观看 | 三级久久久 | 日本女优在线看 | 五月激情丁香 | 国产精品成人国产乱 | 69re视频| 成人精品一区二区三区在线 | 三级视频在线观看 | 快射视频网 | 日本中文字幕在线观看视频 | 亚洲综合在线中文字幕 | 精品久久久久久久免费人妻 | av天天色| 欧美3p在线观看 | 天天艹天天 | 天天爱av | 久久久久久麻豆 | 日韩精品观看 | 日精品 | 免费在线观看一区二区三区 | 打屁股疼的撕心裂肺的视频 | 久久中字| 国产精品免费网站 | 日本一区二区免费在线观看 | 波多野结衣伦理 | 性巴克成人免费网站 | 精品一区精品二区 | 色污网站| 在线观看 亚洲 | aa片在线观看视频在线播放 | 免费看亚洲 | 天天插夜夜操 | 国产农村妇女精品一二区 | 另类亚洲激情 | 天天狠天天干 | 黄色免费网站在线看 | 久久99热这里只频精品6学生 | 毛片自拍 | 亚洲专区视频 | 丁香激情网 | 另类视频在线观看+1080p | 日本高清有码视频 | 五月天中文字幕mv在线 | 国产欧美日韩激情 | h片免费观看 | 一区二区在线观看av | 成人看的视频 | 亚洲成av人片一区二区梦乃 | 成人公开免费视频 | 国产精品九色 | 成人在线免费高清视频 | 久久久久久久国产精品毛片 | 日韩大片免费在线观看 | 91九色国产在线 | 91久久精品一区 | 成人午夜毛片 | 色99999| av基地| 日韩免费久久 | 亚洲天堂中文字幕在线观看 | 亚洲成人精品视频 | 好吊日免费视频 | 欧美视频xxx| 东北老女人av | 91精品综合久久 | 国产香蕉久久 | 一区二区亚洲视频 | 日本特级黄色片 | 亚洲精品在线中文字幕 | 色哟哟网站 | 精品国产鲁一鲁一区二区三区 | 国产春色 | 色婷婷中文 | 亚洲综合精品国产一区二区三区 | 天天操天天爱天天干 | 日韩在线观看视频网站 | 成人听书哪个软件好 | 国产欧美日韩在线视频 | 亚洲免费播放 | 欧美一区自拍 | 国产性生活视频 | 黄色免费网站在线观看 | 黄色免费看网站 | 国产在线三区 | 在线h片 | 成年人免费视频网站 |