1

I'm able to load controllers from external assemblies in MVC6, due to how it finds controllers. But I am unable to render a view that is also in that assembly, as it says it can not be found. The assembly that contains the views and controllers looks as so:

The project it's imported into finds the controller find, and executes the code inside it fine, but then fails finding the view, showing this error

Alexander Forbes-Reed
  • 2,823
  • 4
  • 27
  • 44

3 Answers3

2

If you look at the error, you will see that it tries to find the views under Views directory which is relative to application path. However, you views live under Mvc/Views path.

You can try to override this. I am not sure what is the easiest and proper way to do this but the first think that comes to my mind is that you can provide a FileProvider here on RazorViewEngineOptions which sees /Mvc as the root folder. Here is an example on how you can configure this.

tugberk
  • 57,477
  • 67
  • 243
  • 335
2

To load views from a separate assembly, you need EmbeddedFileProvider and CompositeFileProvider. I provide more information on how to use these here: https://stackoverflow.com/a/34366119/188740

Community
  • 1
  • 1
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
1

I ended up solving this in, what I believe to be, an incredibly hacky way.

Since the views are in each project, inside the src folder, I changed the RazorViewEngine root from the Mvc application, in the src\Branch.Web, to just src. So every view has to be prefixed with the Project name, such as Branch.Game.Halo4. To enable this I just execute this code in the ConfigureServices in Startup.cs

    services.AddMvc().ConfigureRazorViewEngine(options =>
    {
        var oldRoot = ApplicationEnviroment.ApplicationBasePath;
        var trimmedRoot = oldRoot.Remove(oldRoot.LastIndexOf('\\'));

        options.FileProvider = new PhysicalFileProvider(trimmedRoot);
    });

It isn't perfect, but it works.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alexander Forbes-Reed
  • 2,823
  • 4
  • 27
  • 44