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.)