ch1seL.RazorViewRender 6.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ch1seL.RazorViewRender --version 6.0.0
NuGet\Install-Package ch1seL.RazorViewRender -Version 6.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ch1seL.RazorViewRender" Version="6.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ch1seL.RazorViewRender --version 6.0.0
#r "nuget: ch1seL.RazorViewRender, 6.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ch1seL.RazorViewRender as a Cake Addin
#addin nuget:?package=ch1seL.RazorViewRender&version=6.0.0

// Install ch1seL.RazorViewRender as a Cake Tool
#tool nuget:?package=ch1seL.RazorViewRender&version=6.0.0

Build Test Publish

ch1seL.RazorViewRenderService

Easy way to render Razor View to string. It might be use to compose mail message or other texts through Razor Views.

How to use

Installation

Nuget Package Manager Console:
Install-Package ch1seL.RazorViewRenderService
.Net CLI:
dotnet add package ch1seL.RazorViewRenderService

Sample using

Create blank ASP.NET Core Web Application MVC:

dotnet new mvc -n RazorViewRender.Sample

Install Nuget package

dotnet add package ch1seL.RazorViewRenderService

Update Startup.cs:

using ch1seL.RazorViewRender;
...
        public void ConfigureServices(IServiceCollection services)
        {
                ...
                services.AddRazorViewRenderService();          
        }
...

Add template model class Models/FooBarModel.cs

namespace RazorViewRender.Sample.Models
{
        public class FooBarModel
        {
                public string Foo { get; set; }
                public string Bar { get; set; }
        }
}

Add razor template Views/Home/FooBar.cshtml

@model RazorViewRender.Sample.Models.FooBarModel

<!DOCTYPE html>

<html>
<head>
    <title>@Model.Foo</title>
</head>
<body>
<div>
    @Html.Raw(Model.Bar)
</div>
</body>
</html>

Update Controllers/HomeController.cs Add RazorViewRenderService as DI Add FooBar method to compose html-string content

using ch1seL.RazorViewRender;
...
        private readonly RazorViewRenderService _razorViewRenderService;

        public HomeController(ILogger<HomeController> logger, RazorViewRenderService razorViewRenderService)
        {
            _logger = logger;
            _razorViewRenderService = razorViewRenderService;
        }

        public async Task<IActionResult> FooBar()
        {
            var content = await _razorViewRenderService.RenderStringAsync("Home/FooBar",
                new FooBarModel
                {
                    Foo = "Foo", 
                    Bar = "<b>B</b>ar"
                });

            return new ContentResult()
            {
                Content = content,
                ContentType = "text/html",
                StatusCode = 200
            };
        }     
...   

Run application

dotnet run 

Open site https://localhost:5001/Home/FooBar to get result

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0 105 4/10/2024
6.0.0 827 11/17/2021
5.0.0.3 810 11/10/2020
5.0.0.2 373 11/10/2020
5.0.0.1 496 11/10/2020
5.0.0 348 11/10/2020
3.1.0 545 1/10/2020