在 .NET Core 的 MVC 之外使用 Razor [英] Using Razor outside of MVC in .NET Core

查看:20
本文介绍了在 .NET Core 的 MVC 之外使用 Razor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我用 .NET Core 编写的 .NET 控制台应用程序中使用 Razor 作为模板引擎.

I would like to use Razor as a templating engine in a .NET console application that I'm writing in .NET Core.

我遇到的独立 Razor 引擎(RazorEngine、RazorTemplates)都需要完整的 .NET.我正在寻找适用于 .NET Core 的解决方案.

The standalone Razor engines I've come across (RazorEngine, RazorTemplates) all require full .NET. I'm looking for a solution that works with .NET Core.

推荐答案

最近我创建了一个名为 RazorLight.

Recently I've created a library called RazorLight.

它没有多余的依赖项,如 ASP.NET MVC 部分,可以在控制台应用程序中使用.目前它仅支持 .NET Core (NetStandard1.6) - 但这正是您所需要的.

It has no redundant dependencies, like ASP.NET MVC parts and can be used in console applications. For now it only supports .NET Core (NetStandard1.6) - but that's exactly what you need.

这是一个简短的例子:

IRazorLightEngine engine = EngineFactory.CreatePhysical("Path-to-your-views");

// Files and strong models
string resultFromFile = engine.Parse("Test.cshtml", new Model("SomeData")); 

// Strings and anonymous models
string stringResult = engine.ParseString("Hello @Model.Name", new { Name = "John" }); 

这篇关于在 .NET Core 的 MVC 之外使用 Razor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆