Fügen Sie die folgende Klasse Ihres ASP.NET-Core MVC-Projekt:
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
public class CustomCompilationService : DefaultRoslynCompilationService, ICompilationService
{
public CustomCompilationService(ApplicationPartManager partManager,
IOptions<RazorViewEngineOptions> optionsAccessor,
IRazorViewEngineFileProviderAccessor fileProviderAccessor,
ILoggerFactory loggerFactory)
: base(partManager, optionsAccessor, fileProviderAccessor, loggerFactory)
{
}
CompilationResult ICompilationService.Compile(RelativeFileInfo fileInfo,
string compilationContent)
{
return base.Compile(fileInfo, compilationContent);
}
}
Aufschalten des ICompilationService
von MVC mit der obigen Klasse hinzugefügt;
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<ICompilationService, CustomCompilationService>();
}
einen Haltepunkt auf die Methode Compile
Set CustomCompilationService
und Blick compilationContent
.
Hinweise
anzeigen Lookups sind Groß- und Kleinschreibung. Wenn Ihr Controller-Routing eine Ansicht Index
genannt sucht (Index.cshtml
), aber Sie haben Ihre Ansicht Datei index
(index.cshtml
) genannt, erhalten Sie eine Ausnahme erhalten:
InvalidOperationException: Die Ansicht ‚Index‘ wurde nicht gefunden.
perfekte Antwort. vielen Dank. – citykid
Was ist mit ASP.NET Core 2.0? – derigel
@SinglePackAbs yep, es gibt weder DefaultRoslynCompilationService noch ICompilationService. – derigel