I am using ASP.NET Core 1.0.1. I have the following
\"Microsoft.AspNetCore.Mvc\": \"1.0.1\" in order to develo
@Fabricio Koch
How does it work on following code, if i publish Enabling Trimming option in dotnet 5.
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().AddApplicationPart(Assembly.Load(new AssemblyName("ClassLibrary")));
}
Maybe you're doing something wrong. So, here are the steps to make this work.
Add the MVC dependency in the project.json from the ClassLibrary project:
"dependencies": {
"NETStandard.Library": "1.6.0",
"Microsoft.AspNetCore.Mvc": "1.0.0"
},
Update your TestController.cs to be like this:
[Route("api/[controller]")]
public class TestController : Controller{
[HttpGet]
public IEnumerable<string> Get() {
return new string[] { "test1", "test2" };
}
}
Add the reference to ClassLibrary in your WebAPI Project: right-click on "References"->"Add Reference..." or update your project.json like this:
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"ClassLibrary": "1.0.0-*"
},
Update your Startup.cs ConfigureServices method:
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().AddApplicationPart(Assembly.Load(new AssemblyName("ClassLibrary")));
}
https://curia.me/how-to-use-a-controller-from-another-assembly-in-asp-net-core/
So, this is a more precise way to load a controller rather than the whole assembly.
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc()
.AddApplicationPart(typeof(Namespace.SubNamespace.ControllerName).Assembly);
...
}
you don't need to do anything special in Startup.cs of the main web app, it just needs to reference the class library.
The one trick is that for your controllers to be discovered your class library must directly reference MVC in its project.json file dependencies section:
"Microsoft.AspNetCore.Mvc": "1.0.*"
UPDATE: for MVC app I did not need anything special in my Startup but in one of my api apps I did need it maybe because using attribute routing.
services.AddMvc()
.AddApplicationPart(Assembly.Load(new AssemblyName("CSharp.WebLib")))
;
where CSharp.WebLib is the name of my class library