0

I have a ASP.NET MVC project where I want to add some graphical frontend pages, from another assembly. This issue is similar to this question: How to use a controller in another assembly in ASP.NET Core MVC 2.0?

I've added the suggested code:

services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices();

I've added the sample MVC project that Visual Studio creates, with the "HomeController". And the new HomeController is called. (The sample project is able to display just fine, when I run it standalone.)

But when I run it through my main project, it gives the following exception:

An unhandled exception occurred while processing the request. InvalidOperationException: The view 'Index' was not found. The following locations were searched:

/Views/Home/Index.cshtml

/Views/Shared/Index.cshtml

/Pages/Shared/Index.cshtml

Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable originalLocations)

The Index function from the HomeController is found and triggered. (I can set a breakpoint in it.) It seems that the internal setup from the external assembly isn't performed though. (Or something.) The startup code from the 2nd assembly isn't executed. It prolly should, I guess? What is missing?

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(o => o.AddPolicy("Local", builder =>
    {
        builder.AllowAnyOrigin()
               .AllowAnyMethod()
               .AllowAnyHeader();
    }));
    //...
    services.AddSingleton<IDataviewer>(_dataviewer);
    services.AddSingleton<IUserAccessManager>(_userAccessManager);
    services.AddSingleton<ITokenLinkManager>(_tokenLinkManager);
    services.AddSingleton<IEmailManager>(_emailManager);
    services.AddMvc().AddApplicationPart(typeof(UserAdministration.Program).Assembly).AddControllersAsServices();
}

My Configure looks like this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseCors("Local");
    app.UseMvcWithDefaultRoute();
}

The rest of my code shouldn't be relevant, I think. (It's a rather basic project.)

Illishar
  • 886
  • 1
  • 11
  • 24

1 Answers1

0

I think I've found some of the answer. I might have to add something like this, to my Configure function:

var embeddedFileProvider = new Microsoft.Extensions.FileProviders.EmbeddedFileProvider(
    typeof(UserAdministration.Program).Assembly,
    "UserAdministration.wwwroot"
);
app.UseStaticFiles(new StaticFileOptions{FileProvider = embeddedFileProvider });

This will tell it where to get the static files. This requires that the static files have been sucked into the assembly. This can be done with the following entry in the (assembly) csproj file:

<ItemGroup>
   <EmbeddedResource Include="wwwroot/**" />
</ItemGroup>

Likewise, you have to add the following to the ConfigureServices function, so that it'll know where to look for the View files:

services.Configure<Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions>(options =>
{
    options.FileProviders.Add(new Microsoft.Extensions.FileProviders.EmbeddedFileProvider(typeof(UserAdministration.Program).Assembly));
});

Again, this requires the View files to be sucked into the assembly:

<ItemGroup>
   <EmbeddedResource Include="Views/**/*.cshtml" />
</ItemGroup>

Together with the original addition:

services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices();

... it seems be working.

Illishar
  • 886
  • 1
  • 11
  • 24