Nancy - Razor 视图引擎

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.

以上是关于Nancy - Razor 视图引擎的主要内容,如果未能解决你的问题,请参考以下文章

Razor视图引擎

MVC-Razor视图引擎及解决路径问题的三个方法

Razor视图中的@:和语法

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

如何使用 Razor 视图引擎在 JavaScript 中正确混合 C# 代码。我希望根据 ViewBag 中的值包含或省略 Javascript

使用Razor视图引擎来生成邮件内容