Nancy视图引擎(View Engines)
Nancy View Engines
默认情况下,Nancy附带一个内置的视图引擎,称为SuperSimpleViewEngine,它支持所有必需品(necessities ),如布局(layouts),局部(partials),模型(models),条件(conditions )和迭代(iterations) 。 使用SuperSimpleViewEngine时不需要任何其他依赖项,它可以支持.html和.sshtml两种文件。
新建一个视图
首先创建HomeModule.cs类并在构造函数中约定一个/Home/Hello路由,返回Hello.html视图
using Nancy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CoreNancy.Module
{
public class HomeModule : NancyModule
{
public HomeModule()
{
Get("/Home/Hello", parameters =>
{
return View["Hello.html"];
});
}
}
}
然后在wwwroot目录下创建/views/Home/目录并在目录下创建Hello.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<h3>Hello,Nancy View Engines</h3>
</body>
</html>
ok,启动,输入路由查看效果,然后报500错误,一气呵成,完美~
仔细看一下错误详情,Nancy找不到Hello.html这个页面;为什么呢?因为Hello.html在/wwwroot/views/Home/目录下。
另外从错误详情上还可以知道下面两点:
1.支持的文件扩展名:
- sshtml
- html
- htm
2.Nancy是如何寻找Hello.html这个文件的:
- views/Home/Hello.html-zh-CN
- views/Home/Hello.html
- Home/Hello.html-zh-CN
- Home/Hello.html
- views/Hello.html-zh-CN
- views/Hello.html
- Hello.html-zh-CN
- Hello.html
从上面的信息可以看出,Nancy寻找文件的顺序是:
- views/【路由中定义的模块名】/【文件名】
- 【路由中定义的模块名】/【文件名】
- views/【文件名】
- 【文件名】
- 以上四点都匹配不到对应文件的话,返回错误页
知道Nancy如何寻找文件后,而我们把Hello.html放在了/wwwroot/views/Home/目录下,那我们可以自定义根目录到wwwroot
自定义网站根目录
首先,创建CustomRootPathProvider 继承 IRootPathProvider:
using Nancy;
using System;
using System.IO;
namespace CoreNancy.CustomRoot
{
public class CustomRootPathProvider : IRootPathProvider
{
public string GetRootPath()
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
}
}
}
然后,创建CustomBootstrapper 继承 DefaultNancyBootstrapper:
using Nancy;
using Nancy.Configuration;
namespace CoreNancy.CustomRoot
{
public class CustomBootstrapper : DefaultNancyBootstrapper
{
protected override IRootPathProvider RootPathProvider
{
get
{
return new CustomRootPathProvider();
}
}
}
}
然后启动(记得设置Hello.html属性:始终复制到输出目录或者如果较新则复制),这个时候可以看到Nancy找到了Hello.html:
前面说到Nancy支持sshtml、html、和htm扩展名,现在把Hello.html重命名为Hello.sshtml,并且路由修改为:
Get("/Home/Hello", parameters =>
{
return View["Hello.sshtml"];
});
此时也是可以成功访问的,不过一定要同时修改以上两点,只修改一点会报500错误。
Super Simple View Engine
超简单视图引擎,也称为SSVE,旨在支持简单的模板场景,因此在其他引擎中有的许多功能可能在这里是不可用的。
语法(Syntax)
@Model[.Parameters]
例子:
public class HomeModule : NancyModule
{
public HomeModule()
{
Get("/Home/Hello", p => { return View["Hello.html"]; });
Get("/Home/{name}", p => { return View["Index.html", new { Name = p.name }]; });
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<p>name : @Model.Name</p>
</body>
</html>
迭代(Iterators)
语法:
@Each[.Parameters]
[@Current[.Parameters]]
@EndEach
例子:
public class HomeModule : NancyModule
{
public HomeModule()
{
Get("/Home/Hello", p => { return View["Hello.html"]; });
Get("/Home/{name}", p => { return View["Index.html", new { Name = p.name }]; });
Get("/Home/Iterators", p =>
{
return View["Iterators.html", new
{
Users = new List<User> {
new User("张三", 18),
new User("李四", 19)
}
}];
});
}
}
public class User
{
public User()
{
}
public User(string name, int age)
{
this.Name = name + string.Empty;
this.Age = age;
}
public string Name { get; set; }
public int Age { get; set; }
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<table border="1">
<tr>
Name
</tr>
<tr>
Age
</tr>
<tbody>
@Each.Users
<tr>
<td>
@Current.Name
</td>
<td>
@Current.Age
</td>
</tr>
@EndEach
</tbody>
</table>
</body>
</html>
条件(Conditionals)
语法:
@If[Not].Parameters
[contents]
@EndIf
例子:
public class HomeModule : NancyModule
{
public HomeModule()
{
Get("/Home/Hello", p => { return View["Hello.html"]; });
Get("/Home/{name}", p => { return View["Index.html", new { Name = p.name }]; });
Get("/Home/Iterators", p =>
{
return View["Iterators.html", new
{
Users = new List<User> {
new User("张三", 18),
new User("李四", 19)
}
}];
});
Get("/Home/Conditionals", p =>
{
return View["Conditionals.html", new
{
Users = new List<User> {
new User("张三", 18,true),
new User("李四", 19,false)
}
}];
});
}
}
public class User
{
public User()
{
}
public User(string name, int age)
{
this.Name = name + string.Empty;
this.Age = age;
}
public User(string name, int age, bool isShow)
{
this.Name = name + string.Empty;
this.Age = age;
this.IsShow = isShow;
}
public string Name { get; set; }
public int Age { get; set; }
public bool IsShow { get; set; }
}
<table border="1">
<tr>
Name
</tr>
<tr>
Age
</tr>
<tbody>
@Each.Users
@If.IsShow
<tr>
<td>
@Current.Name
</td>
<td>
@Current.Age
</td>
</tr>
@EndIf
@EndEach
</tbody>
</table>
隐含条件(Implicit Conditionals)
语法:
Has[CollectionPropertyName]
HTML Encoding
语法:
@!Model[.Parameter]
@!Current[.Parameter]
例子:
@!Model.Test
@Each
@!Current.Test
@EndEach
Partial View
语法:
@Partial['<view name>'[, Model.Property]]
例子:
Get("/Home/PartialView", p => { return View["PartialView.html"]; });
PartialView.html
<h2 style="color:red;">This is PartialView</h2>
<br />
@Partial['SubPartialView.html']
SubPartialView.html
<h1>This is SubPartialView</h1>
母版页(Master pages and sections)
语法:
@Master['<name>']
@Section['<name>']
@EndSection
例子:
Get("/Home/Master", p => { return View["Master.html", new { Name = "Nancy" }]; });
Master.html
@Master['MasterPage.html']
@Section['Content']
<p>name : @Model.Name</p>
@EndSection
MasterPage.html
<h1>This is MasterPage</h1>
<div style="border:1px solid #000;width:200px;">
@Section['Content'];
</div>
Anti-forgery token
语法
@AntiForgeryToken
例子
@AntiForgeryToken
Path expansion
语法
@Path['<relative-path>']
例子
@Path['~/relative/url/image.png']
代码下载:https://download.****.net/download/qq_33649351/10945491