1. 程式人生 > >Nancy - Razor 視圖引擎

Nancy - Razor 視圖引擎

ati tty eat ppi 基類 sem auto false ()

Nancy裏的Razor引擎是圍繞Razor語法解析器的一個自定義實現。要註意的這個實現與ASP.NET MVC的實現有很多的區別。

記住Nancy縮寫模型用 @Model 而不是 ASP.NET MVC 裏的 @model

安裝 Razor

簡單的引用一下 Nancy.ViewEngines.Razor.dll (可能是 Nancy.ViewEngines.Razornuget安裝) 然後返回 cshtmlvbhtml 文件名結尾的視圖,就是這麽簡單。

配置 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.configweb.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.

Nancy - Razor 視圖引擎