Skip to content

Obsoleting Razor Runtime Compilation Recommendations #63001

@MariovanZeist

Description

@MariovanZeist

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

After reading aspnet/Announcements#522 I am looking for some guidance.

We have created multiple multi-tenant applications that internally utilize Razor views for reporting.

These applications support customer-specific reports that are stored in the database (these reports are custom-made by us)

The ability to recompile a Razor view during production using runtime compilation has been proven invaluable, as we can update reports without having to rebuild and redeploy the application.

In the announcement, the recommended action is to use HOT Reload during development, but there is no mention of how to achieve the same functionality in production.

To get an impression of how we generate the views, we use the IRazorViewEngine and call its RenderAsync method to generate the view

public class RazorTemplateRenderer(IServiceProvider serviceProvider, IRazorViewEngine viewEngine, ITempDataProvider tempDataProvider)
	: IRazorTemplateRenderer
{
	private readonly IServiceProvider _serviceProvider = serviceProvider;
	private readonly IRazorViewEngine _viewEngine = viewEngine;
	private readonly ITempDataProvider _tempDataProvider = tempDataProvider;

	public virtual async Task RenderViewAsync<TModel>(string name, TModel model, Stream output, Encoding encoding, IReadOnlyDictionary<string, object>? values)
	{
		var actionContext = new ActionContext(new DefaultHttpContext { RequestServices = _serviceProvider }, new RouteData(), new ActionDescriptor());
		var view = FindView(actionContext, name);
		using var writer = new StreamWriter(output, encoding, leaveOpen: true);
		var viewData = new ViewDataDictionary<TModel>(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = model };
		values?.ForEach(value => viewData.Add(value.Key, value.Value));
		var viewContext = new ViewContext(actionContext, view, viewData, new TempDataDictionary(actionContext.HttpContext, _tempDataProvider), writer, new HtmlHelperOptions());
		await view.RenderAsync(viewContext);
	}
[...]
}

Describe the solution you'd like

Is there a way we could leverage the HOT reload functionality during runtime in production so we can get similar functionality by providing it a razor.cshtml file, and it generates an IView we can use to generate the HTML?

I am open to other suggestions

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions