Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RazorViewEngineOptions for FileProviders in ASP.NET Core 3.0 #14593

Closed
guardrex opened this issue Sep 25, 2019 — with docs.microsoft.com · 23 comments · Fixed by #15593
Closed

RazorViewEngineOptions for FileProviders in ASP.NET Core 3.0 #14593

guardrex opened this issue Sep 25, 2019 — with docs.microsoft.com · 23 comments · Fixed by #15593
Assignees
Labels
doc-enhancement Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

Copy link
Collaborator

guardrex commented Sep 25, 2019

Moved from #14586 (EDIT by @guardrex to place code block into remark).

@zhou881117 remarks ...

services.Configure(options =>
{
    options.FileProviders.Add(
        new EmbeddedFileProvider(typeof(MySharedController).GetTypeInfo().Assembly));
});

.net core 3.0 RazorViewEngineOptions changed. without FileProviders to locate view

@Rick-Anderson
tks!


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@dotnet-bot dotnet-bot added the Source - Docs.ms Docs Customer feedback via GitHub Issue label Sep 25, 2019
@guardrex
Copy link
Collaborator Author

This topic+sample was updated for 3.0 on #14027.

I'll take a look, but I'm 🏃 at the moment and won't be able to get to this for perhaps a week or two.

@Rick-Anderson
Copy link
Contributor

5.5 K PV/month.

@zhou881117
Copy link

Ah... 3.0 could solve it?

@oleksandrkyselov
Copy link

Perhaps this solution will work.

.NET Core 2 by installing Microsoft.AspNetCore.Mvc.Razor nuget package

services.Configure<RazorViewEngineOptions>(options => {
	options.FileProviders.Clear();
	options.FileProviders.Add(new PhysicalFileProvider(appDirectory));
});

.NET Core 3 by installing Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation nuget package

services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
	options.FileProviders.Clear();
	options.FileProviders.Add(new PhysicalFileProvider(appDirectory));
});

@zhou881117
Copy link

Perhaps this solution will work.

.NET Core 2 by installing Microsoft.AspNetCore.Mvc.Razor nuget package

services.Configure<RazorViewEngineOptions>(options => {
	options.FileProviders.Clear();
	options.FileProviders.Add(new PhysicalFileProvider(appDirectory));
});

.NET Core 3 by installing Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation nuget package

services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
	options.FileProviders.Clear();
	options.FileProviders.Add(new PhysicalFileProvider(appDirectory));
});

services.Configure<Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.MvcRazorRuntimeCompilationOptions>(options =>
{
options.FileProviders.Clear();
options.FileProviders.Add(new PhysicalFileProvider(AppContext.BaseDirectory));
options.FileProviders.Add(new EmbeddedFileProvider(typeof(IntelligentFactory.Web.PluginAdmin.AdminProfile).Assembly));
//new PhysicalFileProvider(appDirectory));
});

MvcRazorRuntimeCompilationOptions is also not work.
may this is not runtime razor.

@nexon33
Copy link

nexon33 commented Oct 6, 2019

for me, MvcRazorRuntimeCompilationOptions also doesn't work, although I can successfully add the code and it throws no error, the views are not found during runtime.

@nexon33
Copy link

nexon33 commented Oct 6, 2019

After a lot of searching managed to solve it, the above code stated is correct. But you will also need to change services.AddRazorPages(); to services.AddRazorPages().AddRazorRuntimeCompilation(); because else you end up configuring something that isn't used anywhere in the project.

@guardrex
Copy link
Collaborator Author

guardrex commented Oct 8, 2019

This is misplaced here ... my bad. This is for the View Complication topic, where some of the updates for 3.0 were made and one update doesn't seem correct based on the announcement. I'll move this issue over and ping all of you on the new issue.

@guardrex guardrex closed this as completed Oct 8, 2019
@guardrex guardrex removed their assignment Oct 8, 2019
@guardrex guardrex removed this from the 2019 Q4 ends Dec 31 milestone Oct 8, 2019
@zhou881117
Copy link

zhou881117 commented Oct 9, 2019

how to run app-parts correct .net core 3.0?
this is my question.

the demo dosen't not work well for .net core 3.0
https://github.com/aspnet/AspNetCore.Docs/tree/5bbb2f5c459cf9bcc1159278a52555cc0f4bb3f6/aspnetcore/mvc/advanced/app-parts
@guardrex

Maybe they are the seem question #14800 ?

@jjxtra
Copy link

jjxtra commented Oct 12, 2019

Trying this but get a ton of errors. I've already put in my csproj the suggested value...

One or more compilation references may be missing. If you're seeing this in a published application, set 'CopyRefAssembliesToPublishDirectory' to true in your project file to ensure files in the refs directory are published.

... thousands of missing type errors ...

@jjxtra
Copy link

jjxtra commented Oct 12, 2019

Going back to .net core 2.2, this is a mess...

@Rick-Anderson
Copy link
Contributor

@jjxtra can you open an issue at https://github.com/aspnet/AspNetCore with the simplest possible repo? This is a closed issue in the doc's repo, not the asp.net core code repo.

@littlesmilelove
Copy link

After a lot of searching managed to solve it, the above code stated is correct. But you will also need to change services.AddRazorPages(); to services.AddRazorPages().AddRazorRuntimeCompilation(); because else you end up configuring something that isn't used anywhere in the project.

After doing that, I get a new exception : Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CompilationFailedException: One or more compilation failures occurred
rq42brze.pko(4,20): error CS0400: The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)

@littlesmilelove
Copy link

how to run app-parts correct .net core 3.0?
this is my question.

the demo dosen't not work well for .net core 3.0
https://github.com/aspnet/AspNetCore.Docs/tree/5bbb2f5c459cf9bcc1159278a52555cc0f4bb3f6/aspnetcore/mvc/advanced/app-parts
@guardrex

Maybe they are the seem question #14800 ?

The same issue, app-parts with razor views can't work in .net core 3.0.

@jjxtra
Copy link

jjxtra commented Oct 19, 2019

There is some voodoo involved, I have solved most of it with this (except unit testing razor) here: dotnet/aspnetcore#14952

@Rick-Anderson
Copy link
Contributor

@Tratcher what's the trick to get the following code to compile in 3.0?

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<RazorViewEngineOptions>(options =>
    {
        options.FileProviders.Add(   // <-------
         new EmbeddedFileProvider(typeof(MySharedController).GetTypeInfo().Assembly));
    });
}

'RazorViewEngineOptions' does not contain a definition for 'FileProviders' and no accessible extension method 'FileProviders' accepting a first argument of type 'RazorViewEngineOptions' could be found

cc @serpent5 @guardrex

@Rick-Anderson
Copy link
Contributor

Never mind
MvcRazorRuntimeCompilationOptions works

@Rick-Anderson
Copy link
Contributor

@serpent5 now I get
The type or namespace name 'MvcRazorRuntimeCompilationOptions' could not be found (are you missing a using directive or an assembly reference?)

@Rick-Anderson
Copy link
Contributor

aspnet/Announcements#343

@serpent5
Copy link
Contributor

serpent5 commented Nov 7, 2019

Have you got it working now?

@Rick-Anderson
Copy link
Contributor

No

@serpent5
Copy link
Contributor

serpent5 commented Nov 7, 2019

You need the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package and these usings:

using System.Reflection;
using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
using Microsoft.Extensions.FileProviders;

Here's the code itself for completeness:

services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
    options.FileProviders.Add(
        new EmbeddedFileProvider(typeof(MySharedController).GetTypeInfo().Assembly));
});

@zhou881117
Copy link

zhou881117 commented Jan 30, 2020

yes,it's ok.

public void ConfigureServices(IServiceCollection services)
{
services.Configure<Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.MvcRazorRuntimeCompilationOptions>(options =>
{
options.FileProviders.Add(
new EmbeddedFileProvider(typeof(MySharedController).GetTypeInfo().Assembly));
});
// Requires using System.Reflection;
var assembly = typeof(MySharedController).GetTypeInfo().Assembly;
services.AddMvc().AddRazorRuntimeCompilation()
.AddApplicationPart(assembly)
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
}

All of

MvcRazorRuntimeCompilationOptions 、AddRazorRuntimeCompilation、EmbeddedResource Include="Views***.cshtml"

https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/mvc/advanced/app-parts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants