0

Problem

When using RazorViewEngine.GetView((executingFilePath: viewPath, viewPath, isMainPage: false) in a WPF application, it can't find the view no matter what path I use.

Setup

  • WPF
  • .NET Core 3.1

What I have tried

When I do this in a console application using .NET Core 3.1, it works fine - it finds the view using the path ~/Views/Templates/MyTemplate.cshtml

This doesn't work when using same approach in a WPF application that targets .NET Core 3.1.

The MyTemplate.cshtml file lives inside Views/Templates in the root of the project.

  • I tried using paths like "~/bin/Debug/netcoreapp3.1/Views /MyTemplate.cshtml" and a bunch of other combinations but still it doesn't work.

  • I tried using the console application as a library and reference it from the WPF application but when I do that I get the same error, I moved all the code from the console application to the WPF project to avoid any cross project reference issues but still it doesn't work.

  • I looked here Razor engine cant find view but no luck.

  • The main difference is that in the WPF application I register the MvcCore services in App.xaml.cs and in the console app I do it on demand e.g: return provider.GetRequiredService<RazorViewToStringRenderer>(); but this should not be an issue.

Code

public App()
{
      var serviceCollection = new ServiceCollection();
      ConfigureServices(serviceCollection);
      ServiceProvider = serviceCollection.BuildServiceProvider();
}

private void OnStartup(object sender, StartupEventArgs e)
{
      var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
      mainWindow.Show();
}

private void ConfigureServices(IServiceCollection services)
{
      var applicationEnvironment = PlatformServices.Default.Application;
      services.AddSingleton(applicationEnvironment);

      var environment = new HostingEnvironment();

      services.AddSingleton<IWebHostEnvironment>(environment);
      var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");
      services.AddSingleton<DiagnosticListener>(diagnosticSource);
      services.AddSingleton<DiagnosticSource>(diagnosticSource);

      services.AddLogging();
      services.AddMvcCore();
      services.AddMvcCore().AddRazorPages();
      services.AddMvcCore().AddRazorViewEngine();
      services.AddMvcCore().AddViews();

      services.AddSingleton<RazorViewToStringRenderer>();
      services.AddSingleton<MainWindow>();
}

public class RazorViewToStringRenderer
{
      private IRazorViewEngine _viewEngine;
      private ITempDataProvider _tempDataProvider;
      private IServiceProvider _serviceProvider;

      public RazorViewToStringRenderer(
          IRazorViewEngine viewEngine,
          ITempDataProvider tempDataProvider,
          IServiceProvider serviceProvider)
      {
        _viewEngine = viewEngine;
        _tempDataProvider = tempDataProvider;
        _serviceProvider = serviceProvider;
      }

      public IView FindView(ActionContext actionContext, string viewName)
      {
         var templatePath= "~/Views/Templates/MyTemplate.cshtml"; //I use this variable for testing but is passed in viewName param.
         var getViewResult = _viewEngine.GetView(executingFilePath:templatePath, viewPath:templatePath, isMainPage: false);
       
         if (getViewResult.Success) //This is always false
         {
           return getViewResult.View;
         }
       .....
       }
}

I'm thinking that I'm missing some configuration in WPF that is preventing the razor pages to be found.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Goca
  • 1,743
  • 2
  • 15
  • 36

0 Answers0