{"id":45015450,"url":"https://github.com/soundaranbu/razor.templating.core","last_synced_at":"2026-02-19T01:01:14.576Z","repository":{"id":40357045,"uuid":"252715276","full_name":"soundaranbu/Razor.Templating.Core","owner":"soundaranbu","description":"Razor Templating Engine to render Razor Views(.cshtml files) to String in Console, Web, Service, Desktop workloads in .NET Core 3+","archived":false,"fork":false,"pushed_at":"2026-01-11T18:23:35.000Z","size":2848,"stargazers_count":376,"open_issues_count":7,"forks_count":52,"subscribers_count":8,"default_branch":"main","last_synced_at":"2026-01-11T21:51:18.654Z","etag":null,"topics":["dotnetcore","dotnetcore-html-templating","email-template","html-template","razor","razor-sdk","razor-templating","razor-view-to-string","renderviewasstring"],"latest_commit_sha":null,"homepage":"https://soundaranbu.medium.com/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soundaranbu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["soundaranbu"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2020-04-03T11:38:42.000Z","updated_at":"2026-01-11T18:23:40.000Z","dependencies_parsed_at":"2024-08-09T17:16:16.104Z","dependency_job_id":"41d18ba8-0f84-4c6a-8105-401d332fc01a","html_url":"https://github.com/soundaranbu/Razor.Templating.Core","commit_stats":{"total_commits":136,"total_committers":9,"mean_commits":15.11111111111111,"dds":0.4191176470588235,"last_synced_commit":"3941bd32b2afa3defe6fb6136512b0339277262f"},"previous_names":["soundaranbu/razortemplating"],"tags_count":27,"template":false,"template_full_name":null,"purl":"pkg:github/soundaranbu/Razor.Templating.Core","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soundaranbu%2FRazor.Templating.Core","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soundaranbu%2FRazor.Templating.Core/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soundaranbu%2FRazor.Templating.Core/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soundaranbu%2FRazor.Templating.Core/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soundaranbu","download_url":"https://codeload.github.com/soundaranbu/Razor.Templating.Core/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soundaranbu%2FRazor.Templating.Core/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29600347,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T00:59:38.239Z","status":"ssl_error","status_checked_at":"2026-02-19T00:59:36.936Z","response_time":162,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotnetcore","dotnetcore-html-templating","email-template","html-template","razor","razor-sdk","razor-templating","razor-view-to-string","renderviewasstring"],"created_at":"2026-02-19T01:01:05.330Z","updated_at":"2026-02-19T01:01:14.501Z","avatar_url":"https://github.com/soundaranbu.png","language":"HTML","funding_links":["https://github.com/sponsors/soundaranbu"],"categories":[],"sub_categories":[],"readme":"# Razor.Templating.Core\n\n[![Build+Test](https://github.com/soundaranbu/Razor.Templating.Core/actions/workflows/dotnetcore.yml/badge.svg?branch=main)](https://github.com/soundaranbu/Razor.Templating.Core/actions/workflows/dotnetcore.yml) ![Downloads](https://img.shields.io/nuget/dt/Razor.Templating.Core) \n![Coverage](https://raw.githubusercontent.com/soundaranbu/Razor.Templating.Core/main/test/Razor.Templating.Core.Test/Reports/badge_shieldsio_linecoverage_green.svg)\n\nUsing [Razor](https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor) for HTML templating has never been easier. Render your .cshtml files to strings easily using this library.\n\nThis library uses precompiled Razor views provided by the [Razor SDK](https://docs.microsoft.com/en-us/aspnet/core/razor-pages/sdk).\n\n## Supported Application Types\n\n|                   | .NET 6 \u0026 Above | .NET 10 \u0026 Above |\n| ----------------- | -------------- | --------------- |\n| Preferred Version | v2.1.0         | 3.0.0           |\n| Console           | \u0026check;        | \u0026check;         |\n| Api               | \u0026check;        | \u0026check;         |\n| Mvc               | \u0026check;        | \u0026check;         |\n| Worker Service    | \u0026check;        | \u0026check;         |\n| WPF               | \u0026check;        | \u0026check;         |\n| WinForms          | \u0026check;        | \u0026check;         |\n| Azure Functions   | \u0026check;        | \u0026check;         |\n\nFor older .NET versions, refer to the [wiki](https://github.com/soundaranbu/Razor.Templating.Core/wiki#historic-versions)\n\n## Supported View Features\n| MVC Razor View Features         |         |\n| ------------------------------- | ------- |\n| ViewModel                       | \u0026check; |\n| ViewBag                         | \u0026check; |\n| ViewData                        | \u0026check; |\n| Layouts                         | \u0026check; |\n| ViewStarts                      | \u0026check; |\n| ViewImports                     | \u0026check; |\n| Partial Views                   | \u0026check; |\n| Tag Helpers                     | \u0026check; |\n| View Components                 | \u0026check; |\n| View Localization (Only MVC)    | \u0026check; |\n| Dependency Injection into Views | \u0026check; |\n| @Url.ContentUrl**               | \u0026cross; |\n| @Url.RouteUrl**                 | \u0026cross; |\n\n**Contributors are welcome who can help to enable these unsupported features.\n\n## Applications\n- Email Templating\n- Report Generation \u0026 more\n\n## Performance\nPerformance of rendering the views to HTML is as fast as rendering the MVC page. The first render may be slightly slower due to the initialization. But, the subsequent renderings are significantly faster. Refer to the benchmark results [here](benchmark/Razor.Template.Core.PerfBenchmark/results/RazorTemplatingCoreBenchMark-report-github.md) and run it for yourself in your own machine to verify the results.\n\n| Method                   |     Mean |    Error |   StdDev |   Gen0 | Allocated |\n| ------------------------ | -------: | -------: | -------: | -----: | --------: |\n| RenderViewWithModelAsync | 28.73 μs | 0.403 μs | 0.357 μs | 5.8594 |     24 KB |\n\n## Runtime Compilation\nFrom .NET 10, the Razor runtime compilation APIs are [marked as obsolete](https://learn.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/10/razor-runtime-compilation-obsolete). As a result, they will be removed from ASP.NET Core in the future. If you're looking to save a Razor view in a database or in a separate folder outside the project and render it on the fly, that scenario is not supported. Microsoft recommends using build-time compilation by including the Razor view in the project and built as part of your app.\n\n\n## Installing the NuGet package\nThis library is available as [NuGet package](https://www.nuget.org/packages/Razor.Templating.Core/)\n\n### Using .NET CLI\n```bash\ndotnet add package Razor.Templating.Core\n```\n### Using Package Reference .csproj\n```bash\n\u003cPackageReference Include=\"Razor.Templating.Core\" Version=\"3.0.0\" /\u003e\n```\n\n## Usage - Render View With Layout\nTo render a view with a layout, model, ViewData, or ViewBag, call `RenderAsync()` on the `RazorTemplateEngine` static class.\n\n### RenderAsync() method\n```csharp\nusing Razor.Templating.Core;\n\nvar model = new ExampleModel()\n{\n    PlainText = \"This text is rendered from Razor Views using Razor.Templating.Core\",\n    HtmlContent = \"\u003cem\u003eYou can use it to generate email content, report generation and so on\u003c/em\u003e\"\n};\n\n// Both ViewBag and ViewData should be added to the same dictionary. \nvar viewDataOrViewBag = new Dictionary\u003cstring, object\u003e();\n// ViewData is the same as in MVC.\nviewDataOrViewBag[\"Value1\"] = \"1\";\n\n// ViewBag.Value2 can be written as shown below. There's no change in how it's accessed in a .cshtml file.\nviewDataOrViewBag[\"Value2\"] = \"2\";\n\nvar html = await RazorTemplateEngine.RenderAsync(\"/Views/ExampleView.cshtml\", model, viewDataOrViewBag);\n```\nBefore using this code, see this article for a sample implementation: https://medium.com/@soundaranbu/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79\n\n\n## Render a View Without a Layout\nIf you need to render a view without a layout, use the `RenderPartialAsync()` method.\n\n### RenderPartialAsync() method\n```cs\nvar html = await RazorTemplateEngine.RenderPartialAsync(\"/Views/ExampleView.cshtml\", model, viewDataOrViewBag);\n```\n\n## Render Views Without Throwing Exceptions\nThere are `TryRenderAsync()` and `TryRenderPartialAsync()` methods which do not throw an exception if the view doesn't exist. Instead, they return a tuple indicating whether the view exists and the rendered string.\n\n### TryRenderAsync() method\n```cs\nvar (viewExists, renderedView) = await engine.TryRenderAsync(\"~/Views/Feature/ExampleViewWithoutViewModel.cshtml\");\n```\n\n### TryRenderPartialAsync() method\n```cs\nvar (viewExists, renderedView) = await engine.TryRenderPartialAsync(\"~/Views/_ExamplePartialView.cshtml\", model);\n```\n\n\n## Razor Views in Library\nYou can organize Razor view files (.cshtml) in a separate shared Razor Class Library (RCL). See a sample library [here](https://github.com/soundaranbu/Razor.Templating.Core/tree/main/examples/RazorTemplates).\n\nThe Razor Class Library's `.csproj` file should look like the example below. The `AddRazorSupportForMvc` property is required.\n\nAlso, the RCL should be referenced by the main project or by any project that invokes rendering methods such as `RazorTemplateEngine.RenderAsync()`.\n```xml\n\u003cProject Sdk=\"Microsoft.NET.Sdk.Razor\"\u003e\n  \u003cPropertyGroup\u003e\n    \u003cTargetFrameworks\u003enet6.0\u003c/TargetFrameworks\u003e\n    \u003cAddRazorSupportForMvc\u003etrue\u003c/AddRazorSupportForMvc\u003e\n  \u003c/PropertyGroup\u003e\n\n  \u003cItemGroup\u003e\n    \u003cFrameworkReference Include=\"Microsoft.AspNetCore.App\" /\u003e\n  \u003c/ItemGroup\u003e\n\u003c/Project\u003e\n```\n\n## Dependency Injection\nDependencies can be injected directly into views using `@inject` in a `.cshtml` file. See a sample application [here](https://github.com/soundaranbu/RazorTemplating/tree/main/examples/Mvc)\n\nIn ASP.NET Core, register dependencies as shown below in `Program.cs`\n```csharp\n...\nbuilder.Services.AddTransient\u003cExampleService\u003e();\n// Add this after registering all other dependencies\nbuilder.Services.AddRazorTemplating();\n```\nor in console or other applications, add as below\n```csharp\nusing Microsoft.Extensions.DependencyInjection;\n\n// Add dependencies to the service collection\nvar services = new ServiceCollection();\nservices.AddTransient\u003cExampleService\u003e();\n// Add RazorTemplating after registering all dependencies\n// This is important for the Razor template engine to find injected services.\nservices.AddRazorTemplating(); \n```\n\nOnce the dependencies are registered, we can use either one of these ways:\n\n### Using `RazorTemplateEngine` static class\n```cs\nvar html = await RazorTemplateEngine.RenderAsync(\"~/Views/ExampleViewServiceInjection.cshtml\");\n```\n\n### Using `IRazorTemplateEngine`\n- Instead of using the `RazorTemplateEngine` static class, you can use the `IRazorTemplateEngine` interface and inject it directly into your class constructor.\n\n```cs\npublic class MyService {\n    private readonly IRazorTemplateEngine _razorTemplateEngine;\n\n    public MyService (IRazorTemplateEngine razorTemplateEngine)\n    {\n        _razorTemplateEngine = razorTemplateEngine;\n    }\n\n    public async Task Index()\n    {\n        var renderedView = await _razorTemplateEngine.RenderAsync(\"/Views/Home/Index.cshtml\");\n        // do something with renderedView\n    }\n}\n```\n\n## Note:\n- Please ensure that view paths are unique across all shared template projects.\n\n## Sample Applications\n Please find the sample applications [here](https://github.com/soundaranbu/RazorTemplating/tree/master/examples) \n \n## Support\nIf you find this helpful, consider supporting development by buying a coffee. Thanks!\n\n[![](https://img.shields.io/static/v1?label=Sponsor\u0026message=%E2%9D%A4\u0026logo=GitHub\u0026color=%23fe8e86\u0026style=for-the-badge\u0026logo=appveyor)](https://github.com/sponsors/soundaranbu)\n\n#### References:\nThanks to all the great articles and projects that helped bring this library to life!\n- https://github.com/Andy9FromSpace/razor-renderer-core\n- https://github.com/aspnet/Entropy/tree/master/samples/Mvc.RenderViewToString\n- https://www.frakkingsweet.com/razor-template-rendering/\n- https://github.com/veccsolutions/RenderRazorConsole\n- https://emilol.com/razor-mailer/\n- https://codeopinion.com/using-razor-in-a-console-application/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoundaranbu%2Frazor.templating.core","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoundaranbu%2Frazor.templating.core","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoundaranbu%2Frazor.templating.core/lists"}