Nancy - Razor 视图引擎

Nancy里的Razor引擎是围绕Razor语法解析器的一个自定义实现。要注意的这个实现与ASP.NET MVC的实现有很多的区别。

记住Nancy缩写模型用 @Model 而不是 ASP.NET MVC 里的 @model

安装 Razor

简单的引用一下 Nancy.ViewEngines.Razor.dll (可能是 Nancy.ViewEngines.Razornuget安装) 然后返回 cshtml 或 vbhtml 文件名结尾的视图,就是这么简单。

配置 Razor

You can specify assemblies and default namespaces that Razor needs to use whilst compiling the views by bootstrapping your own IRazorConfiguration implementation, or defining them in yourweb.config or app.config, thus removing the need to add the @using statements to each view. This step is totally optional if you don‘t require additional references or namespaces in your view.

配置 Razor (使用IRazorConfiguration)

The best approach to configuration is implementing your own IRazorConfiguration, this makes it easy to move between self hosting and hosting in IIS, without having to change anything, since the configuration is part of your code.

实现举例:

public class RazorConfig : IRazorConfiguration
{
    public IEnumerable<string> GetAssemblyNames()
    {
        yield return "HyRes.Models";
        yield return "HyRes.Website";
    }

    public IEnumerable<string> GetDefaultNamespaces()
    {
        yield return "HyRes.Models";
        yield return "HyRes.Website.Infrastructure.Helpers";
    }

    public bool AutoIncludeModelNamespace
    {
        get { return true; }
    }
}

配置 Razor (使用 app.config 或 web.config)

The default IRazorConfiguration implementation (automatically used by Nancy unless explicitly overridden in the bootstrapper) looks in app\web.config (respecitvely app\app.config for non-web projects) in the razor section.

Note: If you‘re self hosting in a Windows Service, Console, WPF App, etc, then the following should be specified in app.config, if you‘re hosting in IIS then you need to specify in web.config

Step 1: Create a custom configuration section

<configSections>
	<section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" />
</configSections>

Step 2: Configure Razor! (note! this is just a sample configuration)

Note: When adding a namespace in your own assembly, you need to specify the assembly also.

<razor disableAutoIncludeModelNamespace="false">
	<assemblies>
		<add assembly="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
		<add assembly="SolrNet" />
		<add assembly="SyslogServerLibrary" />
	</assemblies>
	<namespaces>
		<add namespace="SolrNet" />
		<add namespace="SyslogServerLibrary" />
	</namespaces>
</razor>

Pretty self explanatory except disableAutoIncludeModelNamespace which by default auto references the assembly of the model you pass into the view.

让Razor感知使用的基类型

You can let Razor know which base type your views are using, thus gaining intellisense access to the members of the base type at design-time, by using the @inherits statement in your views. For example you could specify @inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic> to use theNancyRazorViewBase base type with a dynamic model. You need to do this in each of the views where you want the design-time candy.

However there is another way if you have ASP.NET MVC installed. Visual Studio has an intellisense sub-system that can be used to teach Visual Studio about different syntaxes. This sub-system is a bit cumbersome to beat into submission and using it would require us to provide an install for a Nancy toolkit.

ASP.NET MVC has built its own abstraction on top of this sub-system, which is installed as a visual studio extension when you run the ASP.NET MVC installer (it‘s installed in a different folder than the default extensions folder, thus is not shown in the extension manager. Sneaky). If you have ASP.NET MVC installed on the same machine as you are doing Nancy development, you can tap into this abstraction and work for you and your Nancy application.

To do this you need to add the following to your app\web.config file

<configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
</configSections>
<system.web.webPages.razor>
    <pages pageBaseType="Nancy.ViewEngines.Razor.NancyRazorViewBase`1[[System.Object]]">
        <namespaces>
            <add namespace="Nancy.ViewEngines.Razor" />
        </namespaces>
    </pages>
</system.web.webPages.razor>

与Visual Studio 2015交互

When you add a razor page using Visual Studio 2015‘s Add New Item (by right clicking or from menu) and selecting the ‘Web/Razor/Web Page (Razor v3)‘ template, VS will automatically add nuget references to Microsoft.AspNet.WebPages and Microsoft.Web.Infrastructure.

They are not actually needed because we already use Nancy.Viewengines.Razor. To prevent VS from automatically adding those references, don‘t use razor templates when adding the file. Instead add a plain HTML and rename the extension to .cshtml.

时间: 2024-10-14 10:01:39

Nancy - Razor 视图引擎的相关文章

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:/

MVC Razor视图引擎

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

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

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

一.天降神器“剃须刀” — Razor视图引擎 1.1 千呼万唤始出来的MVC3.0 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留下来的ASPX引擎或者第三方的NVelocity模板引擎. Razor在减少代码冗余.增强代码可读性和Visual Studio智能感知方面,都有着突出的优势.Razor一经推出就深受广大ASP.Net开发者的喜爱. 1.2 Razor的语法 (1)Razor文件类型:Razor支持两种文件类型,分

Razor视图引擎语法

@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>RazorView</title> </head> <body> <div> @(ViewBag.cName).cName </div

Razor视图引擎-基础语法

原文:Razor视图引擎-基础语法 所有以 @开头 或 @{ /* 代码体 */ }  (在@与{直接不得添加任何空格) 的部分代码都会被ASP.NET引擎进行处理. 在 @{ /*代码体*/ } 内的代码每一行都必须以";"结束,如 @{ var i = 10; var y = 20; } 而 @xxx 则不需要以";"作为结束符,如 @i 输出 10 @y; 输出 20; 代码区内字母分大小写. 字符类型常量必须用""括起例如: @{ str

Razor视图引擎

在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留下来的ASPX引擎或者第三方的NVelocity模板引擎. (1)Razor文件类型:Razor支持两种文件类型,分别是.cshtml 和.vbhtml,其中.cshtml 的服务器代码使用了c#的语法,.vbhtml 的服务器代码使用了vb.net的语法. (2)@字符:@是Razor中的一个重要符号,它被定义为Razor服务器代码块的开始符号.例如,我们可以在View中直接写C#