An open API service indexing awesome lists of open source software.

https://github.com/quickz/cshtml-class-library-example

An example of how to use ASP.NET Core MVC project views from a razor class library.
https://github.com/quickz/cshtml-class-library-example

asp-net-core asp-net-core-mvc cshtml example razor reuse

Last synced: 6 months ago
JSON representation

An example of how to use ASP.NET Core MVC project views from a razor class library.

Awesome Lists containing this project

README

          

# Cshtml file re-use example

An example of how to use ASP.NET Core MVC project views from a razor class library.

## Enabling run-time compilation

### How to enable run-time compilation for the MVC project
#### 1. Install this NuGet package on to the MVC project
```
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
```

#### 2. Add this on the startup file
```csharp
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
```
image

### How to enable run-time compilation for the razor class library
#### 1. Install this NuGet package on to the razor class library
```
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
```

#### 2. Update MVC project startup file to include this
```csharp
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation(options =>
{
// Used to enable run-time compilation for the razor class library views.
// If removed run-time compilation will only be available for the main project.
if (builder.Environment.IsDevelopment())
{
var path = Path.Combine(
builder.Environment.ContentRootPath,
"../SharedViews");
var fileProvider = new PhysicalFileProvider(path);
options.FileProviders.Add(fileProvider);
}
});
```
image